Skip to content

0.64.0

Latest

Choose a tag to compare

@kateinoigakukun kateinoigakukun released this 27 Nov 09:22
· 1 commit to main since this release
0628da7

This release makes the JavaScript object heap stop deduplicating references. Creating, passing and releasing JSObjects is now 30-50% faster, and the cost no longer grows with the number of live objects.

Breaking changes

JSObject no longer conforms to Hashable

JSObject and its subclasses (JSClosure, JSOneshotClosure, JSSymbol, JSBigInt) are now only Equatable.

Migration

Choose based on what the Set/Dictionary was used for.

// Before
var selected: Set<JSObject> = []
selected.insert(node)
selected.remove(node)

var labels: [JSObject: String] = [:]
labels[node] = "header"
let label = labels[node]

// After
@JSClass(jsName: "Set", from: .global)
struct JSSet {
    @JSFunction init() throws(JSException)
    @JSFunction func add(_ value: JSObject) throws(JSException)
    @JSFunction func delete(_ value: JSObject) throws(JSException) -> Bool
}
let selected = try JSSet()
try selected.add(node)
_ = try selected.delete(node)

@JSClass(jsName: "Map", from: .global)
struct JSMap {
    @JSFunction init() throws(JSException)
    @JSGetter var size: Int
    @JSFunction func get<V: BridgedSwiftGenericBridgeable>(_ key: JSObject) throws(JSException) -> V?
    @JSFunction func set<V: BridgedSwiftGenericBridgeable>(_ key: JSObject, _ value: V) throws(JSException)
}
let labels = try JSMap()
try labels.set(node, "header")
let label: String? = try labels.get(node)

JSObject.== now compares with JavaScript ===

JSObject.== still answers "is this the same JavaScript object?", but it now calls into JavaScript instead of comparing two integers. As a result:

  • It must be called on the thread that owns both objects. This was already asserted for the pair; it is now also asserted for the current thread.
  • It is more expensive than before, so avoid calling it in hot loops.

JSObject.id is no longer unique per JavaScript object (SPI only)

Two JSObjects for the same JavaScript object now have different ids. This only affects code using @_spi(JSObject_id).

Performance

From #835, comparing main before and after the change (release build, Node 22):

Case Before After
Create and drop a JSObject 8.62 ms / 100k 4.48 ms / 100k (−48%)
Read an object property and drop it 44.87 ms / 100k 29.03 ms / 100k (−35%)
Same, with 100k JSObjects alive 38,566 ms / 100k 29.11 ms / 100k
@JSClass create and drop 10.17 ms / 100k 5.08 ms / 100k (−50%)

In the js-framework-benchmark with ElementaryUI, script time dropped by 5–12% for most operations and memory after creating 1k rows dropped by 10.6%.

What's Changed

Full Changelog: 0.63.1...0.64.0