benchmark.js
This file
Path
benchmarks/benchmark.js
83 lines · 1.6 KiB · JavaScript
Last changed by
Started Collection benchmarksvisionmediabefore adoptiondba488d
History of this fileTake it
Started Collection benchmarksvisionmediabefore adoptiondba488d
lines 1–83why, one step back12;(function(){3 var currentSuite4 5 /**6 * Contents of _fn_. Strips function literal and signature.7 *8 * @param {function} fn9 * @return {string}10 * @api private11 */12 13 function contentsOf(fn) {14 return fn.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1]15 }16 17 /**18 * Pad _str_ to _len_.19 *20 * @param {string} str21 * @param {integer} len22 * @return {string}23 * @api private24 */25 26 function pad(str, len) {27 return str + (new Array(len - str.length)).join(' ')28 }29 30 /**31 * Time the execution of _fn_32 *33 * @param {function} fn34 * @return {float}35 * @api private36 */37 38 function time(fn) {39 var start = Number(new Date)40 fn()41 return (Number(new Date) - start) / 100042 }43 44 /**45 * Benchmark _fn_ with the given _label_.46 *47 * @param {string} label48 * @param {function} fn49 * @api public50 */51 52 function benchmark(label, fn) {53 var duration = time(function(){54 for (var i = 0; i < currentSuite.times; ++i)55 fn()56 }).toFixed(3)57 print(pad(' ' + label, 50 - duration.toString().length) + duration + ' |')58 }59 60 /**61 * Create a benchmark suite with the given _label_, which62 * will run each benchmark n _times_. If _times_ is omitted63 * then it defaults to 1.64 *65 * @param {string} label66 * @param {integer, function} times67 * @param {function} fn68 * @api public69 */70 71 suite = function(label, times, fn) {72 currentSuite = this73 if (typeof times == 'function')74 this.times = 1, fn = times75 else76 this.times = times77 print('\n ' + pad(label, 42 - this.times.toString().length) + this.times + ' time(s)')78 print(' -------------------------------------------------')79 eval(contentsOf(fn))80 print('')81 }82 83})()