Last active
August 29, 2015 14:14
-
-
Save aenario/551724be0de2c9600645 to your computer and use it in GitHub Desktop.
This file contains 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
#! /usr/env/node --allow-natives-syntax | |
// Generated by CoffeeScript 1.8.0 | |
(function() { | |
var N, asyncOp, callerEarlyReturn, callerIfElse, getStatus, ntimes, testFN; | |
N = 10000; | |
asyncOp = function(cb) { | |
var err, out; | |
out = Math.random(); | |
if (out > 0.9) { | |
err = new Error('err'); | |
out = null; | |
} else { | |
err = null; | |
} | |
return setTimeout((function() { | |
return cb(err, out); | |
}), 1); | |
}; | |
callerEarlyReturn = function(callback) { | |
return asyncOp(function(err, out) { | |
if (err) { | |
return callback(err); | |
} | |
return callback(null, out * out); | |
}); | |
}; | |
callerIfElse = function(callback) { | |
return asyncOp(function(err, out) { | |
if (err) { | |
return callback(err); | |
} else { | |
return callback(null, out * out); | |
} | |
}); | |
}; | |
ntimes = function(fn, done) { | |
var i, step; | |
i = N; | |
return (step = function() { | |
if (i === 0) { | |
return done(); | |
} else { | |
i -= 1; | |
return fn(function() { | |
return step(); | |
}); | |
} | |
})(); | |
}; | |
testFN = function(code, fn, done) { | |
return fn(function() { | |
console.log('1'); | |
%OptimizeFunctionOnNextCall(fn); | |
return fn(function() { | |
console.log('2'); | |
getStatus(fn); | |
console.time(code); | |
return ntimes(fn, function() { | |
console.timeEnd(code); | |
return done(); | |
}); | |
}); | |
}); | |
}; | |
getStatus = function(fn) { | |
switch (%GetOptimizationStatus(fn)) { | |
case 1: | |
return console.log("Function is optimized"); | |
case 2: | |
return console.log("Function is not optimized"); | |
case 3: | |
return console.log("Function is always optimized"); | |
case 4: | |
return console.log("Function is never optimized"); | |
case 6: | |
return console.log("Function is maybe deoptimized"); | |
} | |
}; | |
testFN('return', callerEarlyReturn, function() { | |
return testFN('ifelse', callerIfElse, function() {}); | |
}); | |
}).call(this); |
This file contains 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
node --allow-natives-syntax early-return-perf-test.js | |
1 | |
2 | |
Function is optimized | |
return: 11835ms | |
1 | |
2 | |
Function is optimized | |
ifelse: 11958ms | |
node --allow-natives-syntax early-return-perf-test.js | |
1 | |
2 | |
Function is optimized | |
return: 11786ms | |
1 | |
2 | |
Function is optimized | |
ifelse: 11892ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment