Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 20, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/ef3ee6dc6c56291d791fe5ce5e8a1cb0 to your computer and use it in GitHub Desktop.
Save miguelmota/ef3ee6dc6c56291d791fe5ce5e8a1cb0 to your computer and use it in GitHub Desktop.
Node.js benchmark.js example
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite
suite.add('fib#recursion', () => {
fibRec(10)
})
.add('fib#loop', () => {
fibLoop(10)
})
.on('complete', () => {
console.log(suite[0].toString())
console.log(suite[1].toString())
})
.run({
async: true
})
function fibRec(n) {
if (n <= 1) {
return n
}
return fibRec(n-1) + fibRec(n-2)
}
function fibLoop(n) {
let f = [0, 1]
for (let i = 2; i <= n; i++) {
f[i] = f[i-1] + f[i-2]
}
return f[n]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment