// Setup code before all tests are ran
jsperf.before(function () {
testStr = '-1/2';
});
// Teardown code after all tests are ran
jsperf.after(function () {
testStr = null;
});
// Setup code before each test (not each iteration)
// jsperf.beforeEach(function () {});
// Teardown code after each test (not each iteration)
// jsperf.beforeEach(function () {});
// A test named 'using split'
jsperf.test('using split', function () {
testStr.split('-')[1];
});
// Another test
jsperf.test('using replace', function () {
testStr.replace('-', '');
});
// An unnamed test
jsperf.test(function () {
if (testStr[0] == '-') testStr.substring(1)
});
// Run for 3 seconds per test. Defaults to 10 seconds.
jsperf.run(3000);
Last active
August 29, 2015 14:27
-
-
Save ryanjduffy/2fbe4aa7908d5a87f98c to your computer and use it in GitHub Desktop.
Console jsperf
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
// copy/paste into console or add as a snippet in the dev tools | |
var jsperf = (function () { | |
var before, beforeEach, after, afterEach, tests; | |
var perfNow = (function () { | |
var perf = window.performance || {}; | |
perf.now = perf.now || perf.mozNow || perf.msNow || perf.oNow || perf.webkitNow || Date.now || function () { | |
return new Date().getTime(); | |
}; | |
return function () { | |
return perf.now(); | |
}; | |
})(); | |
function out (results) { | |
if (console.table) { | |
console.table(results); | |
} else { | |
results.forEach(function (r, i) { | |
console.log(i + '\t' + r.iterations); | |
}); | |
} | |
}; | |
function init () { | |
before = beforeEach = after = afterEach = null; | |
tests = []; | |
} | |
function setBefore (fn) { before = fn; } | |
function setBeforeEach (fn) { beforeEach = fn; } | |
function setAfter (fn) { after = fn; } | |
function setAfterEach (fn) { afterEach = fn; } | |
function addTest (name, fn) { | |
if (!fn) { | |
fn = name; | |
name = 'Test #' + (tests.length + 1); | |
} | |
tests.push({ | |
name: name, | |
fn: fn | |
}); | |
} | |
function metrics (results) { | |
var max = Math.max.apply(Math, results.map(function (r) { return r.Iterations; })); | |
results.forEach(function (r) { | |
var diff = Math.round(((max - r.Iterations) / max) * 100, 2); | |
r.Compare = diff === 0 ? 'Fastest' : diff + '% Slower'; | |
}); | |
} | |
function run (duration) { | |
var start, startEach, endEach, test, result, | |
i = 0, | |
l = tests.length, | |
results = [], | |
duration = duration || 10*1000; | |
if (!l) { | |
console.error('No tests provided'); | |
return; | |
} | |
start = perfNow(); | |
if (before) before(); | |
while (i++ < l) { | |
test = tests.shift(); | |
result = { | |
'Test Name': test.name, | |
Iterations: 0, | |
Compare: '', | |
Duration: 0 | |
}; | |
console.log('Running', test.name); | |
if (beforeEach) beforeEach(); | |
startEach = perfNow(); | |
do { | |
test.fn(); | |
endEach = perfNow(); | |
result.Iterations++; | |
} while (startEach + duration > endEach) | |
if (afterEach) afterEach(); | |
result.Duration = endEach - startEach; | |
results.push(result); | |
} | |
if (after) after(); | |
metrics(results); | |
out(results); | |
init(); | |
} | |
init(); | |
return { | |
before: setBefore, | |
beforeEach: setBeforeEach, | |
after: setAfter, | |
afterEach: setAfterEach, | |
test: addTest, | |
run: run | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment