Skip to content

Instantly share code, notes, and snippets.

@bga
Created October 19, 2010 13:07
Show Gist options
  • Save bga/634158 to your computer and use it in GitHub Desktop.
Save bga/634158 to your computer and use it in GitHub Desktop.
/*
This test detects if engine cache bytecode or not.
*/
var _main = function()
{
var bigCode = String(_main);
bigCode = bigCode.slice(bigCode.indexOf('{') + 1, bigCode.lastIndexOf('}'));
bigCode = new Array(11).join(bigCode); // *= 10 to make really big code
bigCode += 'return ';
_speedTest(
[
function(n)
{
var bigCodeC = bigCode;
var _random = Math.random;
var i = n; while(i--)
{
new Function(bigCodeC + _random());
}
},
function(n)
{
var bigCodeC = bigCode;
var _random = Math.random;
var i = n; while(i--)
{
new Function(bigCodeC + '0.10101010');
'' + _random();
}
},
(function()
{
var fnCache = {};
return function(n)
{
var bigCodeC = bigCode;
var _random = Math.random;
var i = n; while(i--)
{
var code = bigCodeC + '0.10101010';
fnCache[code] || (fnCache[code] = new Function(code));
'' + _random();
}
}
})();
],
1000
);
}; // _main
/*
chrome8
0: 2648 ms
1: 376 ms
2: 385 ms
opera10.62
0: 3372 ms
1: 3304 ms
2: 12 ms
ff3.6
0: 2892 ms
1: 3028 ms
2: 117 ms
ff4
0: 3322 ms
1: 3490 ms
2: 118 ms
ie8
0: 1221 ms
1: 1252 ms
2: 150 ms
safari5.0.2
0: 1880 ms
1: 1896 ms
2: 146 ms
*/
/*
As you see all engines hasnt bytecode cache except V8, but V8 internal bytecode cache speed similar artificial.
But do we need bytecode cache? I guess - no.
Because <new Function> is used very rarely and same code may be is passed in 0.1% cases.
Also bytecode cache requires smart garbage collection mechanism to prevent unefficient memory usage.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment