Upcoming release (v 0.10) has a significant breaking change - in order to bring ... Pure Go JS script support 🎉 #155
Replies: 2 comments 9 replies
-
|
Initial tests seems that runtime performance is slightly slower with Sobek than V8. One significant difference is that V8 supports "templates", so I don't need to create all objects in global scope for every single script context. Sobek/Goja has "templates" internally, but the functionality is unexported. Having templates available would be able to reduce significant browser configuration overhead. |
Beta Was this translation helpful? Give feedback.
-
|
I have started trying to use gost-dom in an... unorthodox... way and am facing some perf issues. I'm trying to make my server-side html templates with JS template literals, rather than using, say, Go's html/template package, templ, or something else. Its surely a novel idea, though I don't think it is crazy - its just a templating engine (could even be jinja, handlebars, or anything else), but hopefully comes with the benefit that the same isomorphic templates could be used sparingly within the browser (eg in a Service Worker or Shared Worker context, when offline). I set up some basic templates and tried rendering with gost-dom's sobekengine, as well as with sobek directly, and notice that the latter is 10-20x faster. Some profiling suggests that the overhead is largely related to the polyfill and other dom api instantiation every time you use browser.Open. I suppose this affects any usage of gost-dom, not just my weird approach. Perhaps it would be best to just use Sobek directly, but I dont think it would have support for the event loop and other things that gost-dom has added on top of it. I would then use full gost-dom for doing actual testing of what happens when the ssr html + associated js gets executed in the browser. Tests would be run far less frequently, and wouldnt be quite as time-sensitive as rendering responses for actual requests You mentioned above
I tried futzing with gost-dom and sobek for a bit to try to get this to work, but don't have a sufficient understanding of either to make it happen. Is this something that you might be able to work on implementing, such that those polyfills etc are only instantiated once and then we can reuse them all (or even the entire browser instance) across various renders? Alternatively (or perhaps in addition) to that, I wonder if there could be a way to specify which polyfills, dom apis etc you want to use - such that in this sort of SSR context, i'd probably use none of them as I just need JS features. This all seems aligned with what you said above
And somewhat the reverse of the Thanks! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the near future, a new release will bring some major changes to the script engine, including pure Go support using grafana/sobek
Sobek support 🍾
The first version of Gost-DOM had an integrated V8. While I think this was quite the achievement, V8 does come with a huge overhead; being written in C++ it depends on CGo, and has a hefty compilation time. Also, while JS itself is garbage collected, the JS GC is not connected to the Go GC. Currently, Gost-DOM will leak memory in the scope of a web page.
I'm proud to announce that a pure Go alternative is now available sobek as an alternate JS engine. Sobek is a fork of goja adding ESM support.
Sobek still needs to be battle tested, but it will at least fix the GC problem of V8.
Note: V8 support will stay, as I can be virtually certain that V8 will continue to be maintained, and match the capabilities of the browser environment that Gost-DOM simulates.
Breaking changes
A script engine will no longer be configured by default
browser.Newwas to configure a script engine if not explicitly configured (falling back to V8)browser.New, an explicit script engine must be supported (see below)To create a browser with a script engine, call:
browser.New(browser.WithEngine(v8engine.DefaultEngine()))browser.New(browser.WithEngine(sobekengine.DefaultEngine()))A temporary helper exists in
v8browser.New- behaves almose as the originalbrowser.New. Exception is that an explicitly configured ScriptEngine is ignored; it will always usev8engine.DefaultEngine()Note
A script engine is not required when creating a browser.
<script>tags will just not do anything.ScriptEngineabstraction layerA script engine is the top layer representing a script engine. The script engine is configured with the web APIs to be accessible. Script engines are supposed to be reused. as they can cache expensive resources. E.g., the V8 engine caches V8 isolates, avoiding configuring root scope for each tests. (v8 has cached isolates for a long time, but in global scope, not scoped to a runtime value)
The script engine creates "hosts" and "contexts" as needed. This should be seen as implementation details from the caller, and may be changed.
V8 moved to new package
As the new top level abstraction is a "script engine", not "script host", the new package name for v8 is
scripting/v8engine, not scripting/v8host`.Most deprecated functions are removed
Most code marked with
// deprecated:has been removed. An exception isClock.RunAll()as it's not completely clear what is a sensible interface.The grand plan
The changes here support to points in the greater scheme
Move script engines
Instead of having a script engine in, e.g.
gost-dom/browser/scripting/v8hostyou would havegost-dom/v8engineas a separate module.This reduces the module dependencies of client code. You only get module dependencies to v8go if you really want the v8 engine.
Allow client code to add new Web APIs
The ScriptEngine adds a layer of abstraction currently only accessible through
internalAPIs, but all Web API implementation is currently independent of the actual script engine.It is the overall goal that Gost-DOM provides core browser functionality, e.g., the DOM and HTML DOM and Network functionality. But 3rd party libraries can provide support for other Web APIs, e.g. Geolocation, Passkey, Local storage (yes, no local storage yet).
The API for configuring this is currently
internal, but I believe it will soon be ready for experimental release.Kudos
Whenever I mention V8, I have must give a shoutout to
SymbolsupportBeta Was this translation helpful? Give feedback.
All reactions