Skip to content

Instantly share code, notes, and snippets.

@stash
Created October 5, 2011 15:31
Show Gist options
  • Select an option

  • Save stash/1264732 to your computer and use it in GitHub Desktop.

Select an option

Save stash/1264732 to your computer and use it in GitHub Desktop.
Logging with runtime level selection in node.js
var N = process.argv[2] || 1000000;
function log () {
console.log.apply(console,arguments);
}
for (var i=0; i<N; i++) {
log("this is message"+i);
}
// didn't run to completion, stopped at ~5m18s
// real 5m18.449s
// user 5m15.645s
// sys 0m5.304s
var LOG = false;
var N = process.argv[2] || 1000000;
function log () {
if (!LOG) return;
console.log.apply(console,arguments);
}
for (var i=0; i<N; i++) {
log("this is message"+i);
}
// real 0m0.331s
// user 0m0.294s
// sys 0m0.028s
var LOG = false;
var N = process.argv[2] || 1000000;
function log () {
console.log.apply(console,arguments);
}
for (var i=0; i<N; i++) {
LOG && log("this is message"+i);
}
// real 0m0.117s
// user 0m0.094s
// sys 0m0.016s
var LOG = false;
var N = process.argv[2] || 1000000;
function log () {
console.log.apply(console,arguments);
}
if (!LOG) log = function(){};
for (var i=0; i<N; i++) {
log("this is message"+i);
}
// real 0m0.311s
// user 0m0.272s
// sys 0m0.027s
@stash
Copy link
Copy Markdown
Author

stash commented Oct 5, 2011

Ran with time (node file.js >/dev/null 2>&1), took the worst times out of 4 consecutive runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment