NeilFraser / JS-Interpreter

A sandboxed JavaScript interpreter in JavaScript.
Apache License 2.0
2.01k stars 353 forks source link

Add size calculation demo #245

Closed NeilFraser closed 1 year ago

cpcallen commented 1 year ago

If allowing ES6, why not use a Map instead of a Set, to make object number lookup faster in encodeValue_?

NeilFraser commented 1 year ago

If allowing ES6, why not use a Map instead of a Set, to make object number lookup faster in encodeValue_?

That's a really good idea. I implemented it just now, and found zero performance improvement in either Chrome or Firefox. The data was very noisy due to the non-deterministic nature of the JIT, but I saw hints that it might actually be very slightly slower. My only guess is that the overhead of a Map vs a Set exactly offsets the gain of using Map.get vs Array.indexOf.

NeilFraser commented 1 year ago

Never mind, it's only a wash if one has a minimal number of objects (~4000). Larger environments (> 10,000) do show a significant speed-up. Changed. https://github.com/NeilFraser/JS-Interpreter/commit/2835162fb5b10bc124ef040a2914d9f7ef9eeba2

cpcallen commented 1 year ago

I note that I made this change in Code City back in 2018 in google/CodeCity#242; regrettably I did not at the time record whether it made a measurable difference to serialisation.