Skip to content

Instantly share code, notes, and snippets.

@caiogondim
Created March 7, 2016 20:15
Show Gist options
  • Save caiogondim/5e8334f7a70974e37590 to your computer and use it in GitHub Desktop.
Save caiogondim/5e8334f7a70974e37590 to your computer and use it in GitHub Desktop.
'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