Skip to content

Instantly share code, notes, and snippets.

@brendanashworth
Last active October 8, 2015 03:26
Show Gist options
  • Save brendanashworth/be02fcadce8f441a7a3b to your computer and use it in GitHub Desktop.
Save brendanashworth/be02fcadce8f441a7a3b to your computer and use it in GitHub Desktop.
es6 arrow functions vs regular functions
'use strict';
var callarrow = () => { return 1 };
var arrow = () => {
var j = 0;
for (var i = 0; i < 100; i++) {
j += callarrow();
}
return j;
}
var callregular = function() { return 1 };
function regular() {
var j = 0;
for (var i = 0; i < 100; i++) {
j += callregular();
}
return j;
}
function benchmark(n, f) {
var start = process.hrtime();
for (var i = 0; i < n; i++) f();
var end = process.hrtime(start);
console.log("%s took %s ns / op", (f.name || 'function'), ((end[0] * 1e9) + end[1]) / n);
}
benchmark(10000 * 10000, arrow);
benchmark(10000 * 10000, regular);
$ node
> process.version
'v4.0.0'
> process.versions.v8
'4.5.103.30'
$ node benchmark/functions
function took 145.01693226 ns / op
regular took 148.5491975 ns / op
$ node benchmark/functions
function took 144.78813348 ns / op
regular took 150.01348607 ns / op
$ node benchmark/functions
function took 147.5060453 ns / op
regular took 150.76537589 ns / op
$ node benchmark/functions
function took 147.22545045 ns / op
regular took 150.65283979 ns / op
$ node benchmark/functions
function took 146.93254574 ns / op
regular took 152.10227278 ns / op
@ronkorving
Copy link

I wonder if all these function calls don't just get optimized away...

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