Code compilation alternative to Function()

Gustavs Tēbergs fixplzsecrets at gmail.com
Mon Jan 20 13:21:08 PST 2014


Following up with a benchmark.

(I thought of this idea while working on a parser generator library,
but unfortunately for my argument the library turned out really
fast...)

I decided to write a converter for Asm.js code to see roughly what
happens when building code expression by expression, and it gives me
existing programs to test with.

https://gist.github.com/fixplz/8529003

Results:

* I took Asm.js code from https://github.com/kripken/ammo.js as a testing source
* Tested with Node 0.10.17 Win7 64
* The JS engine parses the source (1.6 MB) in 180 ms
* The opcode reader traverses the corresponting opcode binary (1.8 MB)
in 120 ms (the binary could be some 30% smaller by adding special case
codes)
* With conversion enabled the opcode reader takes 260 ms to convert back to JS

The output concatenation is very fast. But it could be better.

The conversion forgoes minimizing parentheses so the output is larger
than the source (2.5 MB), so the total time of using a converter like
this would be 260 ms to convert + 300 ms to parse.

That means about 80% of the running time is taken up by conversion to
JS here. If the conversion involved something more complex, like
reading Ruby code, the proportion might decrease.

I think my point stands - this performance is undesirable. It might be
difficult to deploy web apps with dependencies on large foreign
codebases, especially on mobile devices which likely take much longer
to perform all of this.

This is combined with the fact that use of Function() seems to be
discouraged by the CSP proposal.


On the other hand - I learned a bit about execution semantics in V8
and found it effectively does what I wanted, only a little indirectly
by building a string of JS syntax instead of IR. (So the problem
severity in my mind is downgraded from mysterious action to
"Function() kinda sucks".)


More information about the es-discuss mailing list