Skip to content

Instantly share code, notes, and snippets.

@mraleph
Created September 28, 2014 15:04
Show Gist options
  • Save mraleph/463a5138534e6df66f71 to your computer and use it in GitHub Desktop.
Save mraleph/463a5138534e6df66f71 to your computer and use it in GitHub Desktop.
function B1() {
var start = Date.now();
while (...) { /* do N iterations of test body */
/* body of the case goes here {{{ */
function X() { return "hello"; }
// Strictly speaking you can't have FunctionDeclaration as a Statement (see http://es5.github.io/#x12)
// However most implementations support this for compatibility reasons.
// V8 for example hoists all FunctionDeclarations to the top level, just like it does with variable
// declarations. This means in this case X is *not* created again and again everytime the loop is entered.
// Essentially this loop becomes empty.
/* }}} */
}
var end = Date.now();
/* N / (end - start) gives ops per ms result */
}
function B2() {
var start = Date.now();
while (...) { /* do N iterations of test body */
/* body of the case goes here {{{ */
var X = function () { return "hello"; }
// Unlike in the case above V8 would create a new closure evey time function literal expression
// is evaluated. Creating an illusion that FunctionDeclaration above is considerably slower.
// While in fact we are just comparing a loop that does nothing with a loop that does something.
// [V8 also could just eliminate allocation of X but does not do that at them moment]
/* }}} */
}
var end = Date.now();
/* N / (end - start) gives ops per ms result */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment