TopGit tracks dop251/goja on GitHub as part of the Backend family. The project has 7.2k stars. ECMAScript/JavaScript engine in pure Go
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Goja is an implementation of ECMAScript 5.1 in pure Go with emphasis on standard compliance and
performance.
This project was largely inspired by otto.
The minimum required Go version is 1.26.
Features
Full ECMAScript 5.1 support (including regex and strict mode).
Passes nearly all tc39 tests for the features implemented so far. The goal is to
pass all of them. See .tc39_test262_checkout.sh for the latest working commit id.
Capable of running Babel, Typescript compiler and pretty much anything written in ES5.
Sourcemaps.
Most of ES6 functionality, still work in progress, see https://github.com/dop251/goja/milestone/1?closed=1
Known incompatibilities and caveats
JSON
JSON.parse() uses the standard Go library which operates in UTF-8. Therefore, it cannot correctly parse broken UTF-16
surrogate pairs, for example:
JSON.parse(`"\\uD800"`).charCodeAt(0).toString(16) // returns "fffd" instead of "d800"
Date
Conversion from calendar date to epoch timestamp uses the standard Go library which uses int, rather than float as per
ECMAScript specification. This means if you pass arguments that overflow int to the Date() constructor or if there is
an integer overflow, the result will be incorrect, for example:
Although it's faster than many scripting language implementations in Go I have seen
(for example it's 6-7 times faster than otto on average) it is not a
replacement for V8 or SpiderMonkey or any other general-purpose JavaScript engine.
You can find some benchmarks here.
Why would I want to use it over a V8 wrapper?
It greatly depends on your usage scenario. If most of the work is done in javascript
(for example crypto or any other heavy calculations) you are definitely better off with V8.
If you need a scripting language that drives an engine written in Go so that
you need to make frequent calls between Go and javascript passing complex data structures
then the cgo overhead may outweigh the benefits of having a faster javascript engine.
Because it's written in pure Go there are no cgo dependencies, it's very easy to build and it
should run on any platform supported by Go.
It gives you a much better control over execution environment so can be useful for research.
Is it goroutine-safe?
No. An instance of goja.Runtime can only be used by a single goroutine
at a time. You can create as many instances of Runtime as you like but
it's not possible to pass object values between runtimes.
Where is setTimeout()/setInterval()?
setTimeout() and setInterval() are common functions to provide concurrent execution in ECMAScript environments, but the two functions are not part of the ECMAScript standard.
Browsers and NodeJS just happen to provide similar, but not identical, functions. The hosting application need to control the environment for concurrent execution, e.g. an event loop, and supply the functionality to script code.
There is a separate project aimed at providing some NodeJS functionality,
and it includes an event loop.
Can you implement (feature X from ES6 or higher)?
I will be adding features in their dependency order and as quickly as time permits. Please do not ask
for ETAs. Features that are open in the milestone are either in progress
or will be worked on next.
The ongoing work is done in separate feature branches which are merged into master when appropriate.
Every commit in these branches represents a relatively stable state (i.e. it compiles and passes all enabled tc39 tests),
however because the version of tc39 tests I use is quite old, it may be not as well tested as the ES5.1 functionality. Because there are (usually) no major breaking changes between ECMAScript revisions
it should not break your existing code. You are encouraged to give it a try and report any bugs found. Please do not submit fixes though without discussing it first, as the code could be changed in the meantime.
How do I contribute?
Before submitting a pull request please make sure that:
You followed ECMA standard as close as possible. If adding a new feature make sure you've read the specification,
do not just base it on a couple of examples that work fine.
Your change does not have a significant negative impact on performance (unless it's a bugfix and it's unavoidable)
It passes all relevant tc39 tests.
Current Status
There should be no breaking changes in the API, however it may be extended.
Some of the AnnexB functionality is missing.
Basic Example
Run JavaScript and get the result value.
vm := goja.New()
v, err := vm.RunString("2 + 2")
if err != nil {
panic(err)
}
if num := v.Export().(int64); num != 4 {
panic(num)
}
Passing Values to JS
Any Go value can be passed to JS using Runtime.ToValue() method. See the method's documentation for more details.
Exporting Values from JS
A JS value can be exported into its default Go representation using Value.Export() method.
Alternatively it can be exported into a specific Go variable using Runtime.ExportTo() method.
Within a single export operation the same Object will be represented by the same Go value (either the same map, slice or
a pointer to the same struct). This includes circular objects and makes it possible to export them.
Calling JS functions from Go
There are 2 approaches:
Using AssertFunction():
const SCRIPT = `
function sum(a, b) {
return +a + b;
}
`
vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
panic(err)
}
sum, ok := goja.AssertFunction(vm.Get("sum"))
if !ok {
panic("Not a function")
}
res, err := sum(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
if err != nil {
panic(err)
}
fmt.Println(res)
// Output: 42
Using Runtime.ExportTo():
const SCRIPT = `
function sum(a, b) {
return +a + b;
}
`
vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
panic(err)
}
var sum func(int, int) int
err = vm.ExportTo(vm.Get("sum"), &sum)
if err != nil {
panic(err)
}
fmt.Println(sum(40, 2)) // note, _this_ value in the function will be undefined.
// Output: 42
The first one is more low level and allows specifying this value, whereas the second one makes the function look like
a normal Go function.
Mapping struct field and method names
By default, the names are passed through as is which means they are capitalised. This does not match
the standard JavaScript naming convention, so if you need to make your JS code look more natural or if you are
dealing with a 3rd party library, you can use a FieldNameMapper:
vm := goja.New()
vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
type S struct {
Field int `json:"field"`
}
vm.Set("s", S{Field: 42})
res, _ := vm.RunString(`s.field`) // without the mapper it would have been s.Field
fmt.Println(res.Export())
// Output: 42
There are two standard mappers: TagFieldNameMapper and
UncapFieldNameMapper, or you can use your own implementation.
Native Constructors
In order to implement a constructor function in Go use func (goja.ConstructorCall) *goja.Object.
See Runtime.ToValue() documentation for more details.
Regular Expressions
Goja uses regexp2 library. There are a few known incompatibilities, but they are
mostly edge cases and should not affect general usage. See tc39_test.go for the list of exclusions. Any new
incompatibilities should be reported as bugs.
Exceptions
Any exception thrown in JavaScript is returned as an error of type *Exception. It is possible to extract the value thrown
by using the Value() method:
vm := goja.New()
_, err := vm.RunString(`
throw("Test");
`)
if jserr, ok := err.(*Exception); ok {
if jserr.Value().Export() != "Test" {
panic("wrong value")
}
} else {
panic("wrong type")
}
If a native Go function panics with a Value, it is thrown as a Javascript exception (and therefore can be caught):
var vm *Runtime
func Test() {
panic(vm.ToValue("Error"))
}
vm = goja.New()
vm.Set("Test", Test)
_, err := vm.RunString(`
try {
Test();
} catch(e) {
if (e !== "Error") {
throw e;
}
}
`)
if err != nil {
panic(err)
}
Interrupting
func TestInterrupt(t *testing.T) {
const SCRIPT = `
var i = 0;
for (;;) {
i++;
}
`
vm := goja.New()
time.AfterFunc(200 * time.Millisecond, func() {
vm.Interrupt("halt")
})
_, err := vm.RunString(SCRIPT)
if err == nil {
t.Fatal("Err is nil")
}
// err is of type *InterruptError and its Value() method returns whatever has been passed to vm.Interrupt()
}
NodeJS Compatibility
There is a separate project aimed at providing some of the NodeJS functionality.
The most recent commit recorded on dop251/goja was 13 days ago, based on the GitHub push timestamp. The repository has 516 forks — one of the better signals of community interest.
How many stars does dop251/goja have?
dop251/goja has 7.2k GitHub stars — refresh the page for the live number, or check github.com/dop251/goja. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What else is in the Backend space?
dop251/goja is tracked by TopGit under the Backend category, alongside 2 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What language is dop251/goja written in?
dop251/goja is written primarily in Go. GitHub's language field is based on the largest share of bytes in the default branch.
What topics is dop251/goja associated with?
GitHub's repository topics for dop251/goja: "golang", "javascript". TopGit's editorial category is Backend.
Where do I read more about dop251/goja?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/dop251/goja is the definitive source.
Why is dop251/goja categorized under Backend?
TopGit places dop251/goja in the Backend category based on its GitHub topics and description (tagged: "golang", "javascript"). Categories are assigned from real repository metadata, not editorial guesswork.
Read full README in the tab above.
Want a second opinion on goja?
Ask an AI that can read this page — one click and you get its take on goja.