Skip to content

Instantly share code, notes, and snippets.

@JuhQ
Created December 22, 2014 22:32
Show Gist options
  • Save JuhQ/91940c27d1bde05e42ab to your computer and use it in GitHub Desktop.
Save JuhQ/91940c27d1bde05e42ab to your computer and use it in GitHub Desktop.
js snippet for checking node module performance
var execute = function(src, times) {
var times = times || 10;
var code = require(src);
var results = [];
for(var i = 0; i < times; i++) {
var result = {};
result.iteration = i;
var startTime = new Date().getTime();
code();
var endTime = new Date().getTime();
result.time = endTime - startTime;
results.push(result);
}
var result_length = results.length;
var total_length = 0;
for(var i = 0; i < result_length; i++) {
total_length += results[i].time;
}
var average = total_length / result_length;
console.log("Average execute time", average);
var final_results = {
timestamp: new Date().getTime(),
average: average,
results: results
};
console.log("final_results", final_results);
};
// TODO: fix hardcoded order in arguments
var file_argument = process.argv[2];
var file = file_argument.split("=")[1];
var times_argument = process.argv[3];
if(times_argument) {
var times = times_argument.split("=")[1];
}
execute(file, times);
// How to use:
//
// node performance.js --file=./yourfile.js [--times=50]
// TODO:
// Fix hardcoded argument order
// Save memory usage per iteration
// Save result to json file
// Serve said json to some chart library
// Create grunt/gulp plugins?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment