benchmark.js 463 B

12345678910111213141516
  1. // Runs a function many times without the function call overhead
  2. function benchmark(fn, times, name){
  3. fn = fn.toString();
  4. var s = fn.indexOf('{')+1,
  5. e = fn.lastIndexOf('}');
  6. fn = fn.substring(s,e);
  7. return benchmarkString(fn, times, name);
  8. }
  9. function benchmarkString(fn, times, name) {
  10. var fn = new Function("i", "var t=new Date; while(i--) {" + fn + "}; return new Date - t")(times)
  11. fn.displayName = name || "benchmarked";
  12. return fn;
  13. }