Created
March 15, 2016 09:21
-
-
Save paddybyers/00d178f636cceb7435d4 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
const fs = require('fs'); | |
const util = require('util'); | |
const slice = [].slice; | |
const format = util.format; | |
const unloggedIterationCount = 10000000; | |
const loggedIterationCount = 10000; | |
const fileName = './test.log'; | |
const testObject = { | |
once: 'upon', a: 'time' | |
}; | |
function log_simple(level, msg) { | |
if(level) { | |
fs.appendFileSync(fileName, msg + '\n'); | |
} | |
} | |
function log_closure(level, f) { | |
if(level) { | |
fs.appendFileSync(fileName, f() + '\n'); | |
} | |
} | |
function log_format(level /*, args */) { | |
if(level) { | |
fs.appendFileSync(fileName, format.apply(null, slice.call(arguments, 1)) + '\n'); | |
} | |
} | |
function time_simple() { | |
console.log('Starting test: simple'); | |
console.log('Starting unlogged test: simple'); | |
const unloggedStartTime = Date.now(); | |
for(let i = 0; i < unloggedIterationCount; i++) { | |
log_simple(false, 'This is a simple test message; iteration = ' + i + '; obj = ' + JSON.stringify(testObject)); | |
} | |
const unloggedEndTime = Date.now(); | |
console.log('Ending unlogged test: simple: time elapsed = ' + (unloggedEndTime - unloggedStartTime) + ' ms'); | |
console.log('Starting logged test: simple'); | |
const loggedStartTime = Date.now(); | |
for(let i = 0; i < loggedIterationCount; i++) { | |
log_simple(true, 'This is a simple test message; iteration = ' + i + '; obj = ' + JSON.stringify(testObject)); | |
} | |
const loggedEndTime = Date.now(); | |
console.log('Ending logged test: simple: time elapsed = ' + (loggedEndTime - loggedStartTime) + ' ms'); | |
} | |
function time_closure() { | |
console.log('Starting test: closure'); | |
console.log('Starting unlogged test: closure'); | |
const unloggedStartTime = Date.now(); | |
for(let i = 0; i < unloggedIterationCount; i++) { | |
log_closure(false, () => 'This is a closure test message; iteration = ' + i + '; obj = ' + JSON.stringify(testObject)); | |
} | |
const unloggedEndTime = Date.now(); | |
console.log('Ending unlogged test: closure: time elapsed = ' + (unloggedEndTime - unloggedStartTime) + ' ms'); | |
console.log('Starting logged test: closure'); | |
const loggedStartTime = Date.now(); | |
for(let i = 0; i < loggedIterationCount; i++) { | |
log_closure(true, () => 'This is a closure test message; iteration = ' + i + '; obj = ' + JSON.stringify(testObject)); | |
} | |
const loggedEndTime = Date.now(); | |
console.log('Ending logged test: closure: time elapsed = ' + (loggedEndTime - loggedStartTime) + ' ms'); | |
} | |
function time_format() { | |
console.log('Starting test: format'); | |
console.log('Starting unlogged test: format'); | |
const unloggedStartTime = Date.now(); | |
for(let i = 0; i < unloggedIterationCount; i++) { | |
log_format(false, 'This is a format test message; iteration = %d; obj = %j', i, testObject); | |
} | |
const unloggedEndTime = Date.now(); | |
console.log('Ending unlogged test: format: time elapsed = ' + (unloggedEndTime - unloggedStartTime) + ' ms'); | |
console.log('Starting logged test: format'); | |
const loggedStartTime = Date.now(); | |
for(let i = 0; i < loggedIterationCount; i++) { | |
log_format(true, 'This is a format test message; iteration = %d; obj = %j', i, testObject); | |
} | |
const loggedEndTime = Date.now(); | |
console.log('Ending logged test: format: time elapsed = ' + (loggedEndTime - loggedStartTime) + ' ms'); | |
} | |
time_simple(); | |
time_closure(); | |
time_format(); | |
/****** Results ***** | |
* node log_test.js | |
* Starting test: simple | |
* Starting unlogged test: simple | |
* Ending unlogged test: simple: time elapsed = 9792 ms | |
* Starting logged test: simple | |
* Ending logged test: simple: time elapsed = 665 ms | |
* Starting test: closure | |
* Starting unlogged test: closure | |
* Ending unlogged test: closure: time elapsed = 1846 ms | |
* Starting logged test: closure | |
* Ending logged test: closure: time elapsed = 504 ms | |
* Starting test: format | |
* Starting unlogged test: format | |
* Ending unlogged test: format: time elapsed = 450 ms | |
* Starting logged test: format | |
* Ending logged test: format: time elapsed = 609 ms | |
*****************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment