Created
January 23, 2015 19:37
-
-
Save mraleph/2942a14ef2a480e2a7a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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?