Skip to content

Instantly share code, notes, and snippets.

@spddl
Created June 19, 2019 06:49
Show Gist options
  • Save spddl/bfbb66b9ea799909de179466969cceb8 to your computer and use it in GitHub Desktop.
Save spddl/bfbb66b9ea799909de179466969cceb8 to your computer and use it in GitHub Desktop.
const os = require('os')
const Benchmark = require('benchmark') // https://github.com/bestiejs/benchmark.js
console.log(`cpu: ${os.cpus()[0].model}`)
console.log(`NodeJS version: ${process.version}`)
console.log(`platform: ${os.platform()}`)
console.log()
const suite = new Benchmark.Suite()
const func1 = function (sourceLevel) {
let level
switch (sourceLevel) {
case 1: level = 'VERBOSE'; break
case 2: level = 'DEBUG'; break
case 3: level = 'INFO'; break
case 4: level = 'WARN'; break
case 5: level = 'ERROR'; break
case 6: level = 'FATAL'; break
default: level = 'USERLVL'
}
return level
}
const obj = {
1: 'VERBOSE',
2: 'DEBUG',
3: 'INFO',
4: 'WARN',
5: 'ERROR',
6: 'FATAL'
}
const func2 = function (sourceLevel) {
return obj[sourceLevel]
}
const array = [ null, 'VERBOSE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' ]
const func3 = function (sourceLevel) {
return array[sourceLevel]
}
// add tests
suite
.add('switch', () => { // "switch x 1,181,888 ops/sec ±0.30% (96 runs sampled)"
const a = func1(1)
const e = func1(5)
const d = func1(4)
const c = func1(3)
const b = func1(2)
const f = func1(6)
})
.add('obj', () => { // "obj x 565,235 ops/sec ±0.19% (95 runs sampled)"
const a = func2(1)
const e = func2(5)
const d = func2(4)
const c = func2(3)
const b = func2(2)
const f = func2(6)
})
.add('array', () => { // "array x 538,314 ops/sec ±5.08% (88 runs sampled)"
const a = func3(1)
const e = func3(5)
const d = func3(4)
const c = func3(3)
const b = func3(2)
const f = func3(6)
})
// add listeners
.on('cycle', (event) => {
console.log(String(event.target))
})
.on('complete', () => {
console.log()
console.log('Fastest is ' + suite.filter('fastest').map('name'))
})
// run async
.run({ 'async': true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment