Skip to content

Instantly share code, notes, and snippets.

@Pajn
Last active April 21, 2018 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pajn/1376abe2223031ae732de32f1abe5c90 to your computer and use it in GitHub Desktop.
Save Pajn/1376abe2223031ae732de32f1abe5c90 to your computer and use it in GitHub Desktop.

I'm working on a compile to JS language and when tinkering with the emitter I found some strange performance difference A tuple type in the language is emitted as an JS array and a tuple type constructor is emitted as a function that returns it arguments as an JS array.

I tried these three outputs and discovered unexpectedly big performance differences. The only differences is how the tuple constructors are emitted, and the time is how long it taks for the compiler to build itself.

  • (...members) => members: ~3.6 seconds
  • function () {return Array.prototype.slice.call(arguments)} ~4.6 seconds
  • function () {return Array.from(arguments)} ~5.5 seconds

This is no way a scientific measurement but the time difference seems pretty big for such a small change in the compiler.

I don't know how many times the above functions gets called during the build but the rest of the code is a parser, a type checker and an emitter. There are multiple passes over the AST and it's not like the rest of the code is optimised. Even if there are big differences in a micro benchmark I would not expect this big difference on the total time.

@JakobJingleheimer
Copy link

@Pajn It's probably because the latter 2 rely on a function call, which is generally less performant than a language native. Array.from is just fairly expensive (it does a bunch of checks and coercion/transformation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment