Created
September 7, 2013 02:05
-
-
Save kenany/6472179 to your computer and use it in GitHub Desktop.
factorial in Node.js benchmark
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
| var Benchmark = require('benchmark'); | |
| function factorial1(v) { | |
| return v === 0 ? 1 : v * factorial1(v - 1); | |
| } | |
| function factorial2(v) { | |
| return v ? v * factorial2(v - 1) : 1; | |
| } | |
| var suite = new Benchmark.Suite; | |
| suite | |
| .add('factorial1', function() { | |
| factorial1(21); | |
| }) | |
| .add('factorial2', function() { | |
| factorial2(21); | |
| }) | |
| .on('cycle', function(event) { | |
| console.log(String(event.target)); | |
| }) | |
| .on('complete', function() { | |
| console.log('\nFastest is ' + this.filter('fastest').pluck('name')); | |
| }) | |
| .run({'async': true}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.pluckshould be.mapfor the benchmark 2.1.4.