Skip to content

Commit

Permalink
Added a new command (npm run test-performance) that shows that topiar…
Browse files Browse the repository at this point in the history
…ist is currently ~25 times slower than using typeof and instanceof directly for the most common use case -- verifiying argument types.
  • Loading branch information
dchambers committed Oct 25, 2014
1 parent a703e66 commit 03e51ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"scripts": {
"test": "jasmine-node --verbose spec",
"saucelabs-test": "grunt saucelabs-test"
"saucelabs-test": "grunt saucelabs-test",
"test-performance": "node scripts/test-performance.js"
},
"bundledDependencies": [
"jasmine-standalone"
Expand Down
28 changes: 28 additions & 0 deletions scripts/test-performance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var topiarist = require('../lib/topiarist');

function topiaristFunc(date, num) {
topiarist.isA(date, Date);
topiarist.isA(num, Number);
}

function manualFunc(date, num) {
if(!(date instanceof Date)) throw new Error('date must be of type Date');
if(typeof(num) != 'number') throw new Error('num must be a number');
}

function speedTest(funcName, func) {
var currentDate = new Date();
var startTime = Date.now();

for(var i = 0; i < 1000000; ++i) {
func(currentDate, i);
}

var endTime = Date.now();
console.log(funcName + ': ' + (endTime - startTime));
}

speedTest('topiarist', topiaristFunc);
speedTest('manual', manualFunc);
speedTest('topiarist', topiaristFunc);
speedTest('manual', manualFunc);

0 comments on commit 03e51ba

Please sign in to comment.