Created
March 7, 2016 20:15
-
-
Save caiogondim/5e8334f7a70974e37590 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 _ = require('./underscore') | |
const Benchmark = require('benchmark') | |
const debug = require('logdown')() | |
// Current | |
function fibonacci(n) { | |
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2) | |
} | |
fibonacci = _.memoizeOriginal(fibonacci) | |
// Improved | |
function fibonacci2(n) { | |
return n < 2 ? n : fibonacci2(n - 1) + fibonacci2(n - 2) | |
} | |
fibonacci2 = _.memoize(fibonacci2) | |
// Benchmark suite | |
let suiteFibonnaci = new Benchmark.Suite() | |
let fibNumber = 15 | |
suiteFibonnaci | |
.add('current', () => { | |
fibonacci(fibNumber) | |
}) | |
.add('improved', () => { | |
fibonacci2(fibNumber) | |
}) | |
.on('cycle', (event) => { | |
const currentRunning = String(event.target) | |
.replace(/(.*)\ x/, (match, p1) => `\`${p1}\` x`) | |
debug.log(currentRunning) | |
}) | |
.on('complete', function () { | |
debug.log() | |
debug.log(`Fastest is \`${this.filter('fastest').map('name')}\``) | |
}) | |
.run({'async': true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment