Created
January 20, 2019 08:12
-
-
Save miguelmota/ef3ee6dc6c56291d791fe5ce5e8a1cb0 to your computer and use it in GitHub Desktop.
Node.js benchmark.js example
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
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