Skip to content

Instantly share code, notes, and snippets.

@mraleph
Created January 23, 2015 19:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraleph/2942a14ef2a480e2a7a9 to your computer and use it in GitHub Desktop.
Save mraleph/2942a14ef2a480e2a7a9 to your computer and use it in GitHub Desktop.
// Benefit of optimizing this function via OSR is high:
// a) it is long running;
// b) optimization speeds up the code considerably;
// c) it is called a few times only.
function loopy() {
var arr = new Uint8Array(100);
for (var j = arr[0]; j < 1e2; j++) {
for (var i = arr[0]; i < 1e6; i++) {
Math.sqrt(arr[0] * arr[0]);
}
}
}
var LEAK = process.argv[2] === 'immortalize' ?
new Uint8Array(100) : null;
var start = Date.now();
loopy();
loopy();
new Uint8Array(1e8); // Cause major GC
new Uint8Array(1e8);
new Uint8Array(1e8);
loopy();
var end = Date.now();
console.log(end - start);
//
// This example shows performance characteristics that are in some sense
// inverse to what we saw on Sieve: immortalization of Uint8Array hidden
// class has no effect on the version that uses OSR but has impact
// on the version that does not use OSR.
//
// $ iojs mortality.js
// 420
//
// $ iojs mortality.js immortalize
// 418
//
// $ iojs --nouse-osr mortality.js
// 7903
//
// $ iojs --nouse-osr mortality.js immortalize
// 5449
//
@tssajo
Copy link

tssajo commented May 8, 2015

@mraleph Why does "c) it is called a few times only" matter? What if a function like this gets called a lot of times instead of just "a few times only"? Optimizing a function which gets called a lot should be even more beneficial, no? If not, why not?

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