├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bench.js ├── example.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.12" 5 | - iojs-v1 6 | - iojs-v2 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Matteo Collina 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fastbench  [![Build Status](https://travis-ci.org/mcollina/fastbench.png)](https://travis-ci.org/mcollina/fastbench) 2 | 3 | The simplest benchmark you can run on node 4 | 5 | ## Install 6 | 7 | ```js 8 | npm install fastbench 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | 'use strict' 15 | 16 | var bench = require('fastbench') 17 | 18 | var run = bench([ 19 | function benchSetTimeout (done) { 20 | setTimeout(done, 0) 21 | }, 22 | function benchSetImmediate (done) { 23 | setImmediate(done) 24 | }, 25 | function benchNextTick (done) { 26 | process.nextTick(done) 27 | } 28 | ], 1000) 29 | 30 | // run them two times 31 | run(run) 32 | ``` 33 | 34 | Output: 35 | 36 | ``` 37 | benchSetTimeout*1000: 1363ms 38 | benchSetImmediate*1000: 4ms 39 | benchNextTick*1000: 1ms 40 | benchSetTimeout*1000: 1365ms 41 | benchSetImmediate*1000: 4ms 42 | benchNextTick*1000: 0ms 43 | ``` 44 | 45 | You can disable colors by passing a `--no-color` flag to your node 46 | script. 47 | 48 | ## API 49 | 50 | ### bench(functions, iterations) 51 | 52 | Build a benchmark for the given functions and that precise number of 53 | iterations. It returns a function to run the benchmark. 54 | 55 | The iterations parameter can also be an `Object`, in which case it 56 | acceps two options: 57 | 58 | * `iterations`: the number of iterations (required) 59 | * `max`: is a an alias for iterations 60 | * `color`: if the output should have color (default: true) 61 | 62 | ## License 63 | 64 | MIT 65 | -------------------------------------------------------------------------------- /bench.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var fastseries = require('fastseries') 4 | var chalk = require('chalk') 5 | var colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray'] 6 | var console = require('console') 7 | 8 | function build (functions, opts) { 9 | 10 | var max 11 | var series = fastseries() 12 | var currentColor = 0 13 | 14 | if (typeof opts === 'object') { 15 | max = opts.max || opts.iterations 16 | } else { 17 | max = opts 18 | } 19 | 20 | if (!max) { 21 | throw new Error('missing number of iterations') 22 | } 23 | 24 | return run 25 | 26 | function run (done) { 27 | currentColor = 0 28 | series(null, bench, functions, done || noop) 29 | } 30 | 31 | function bench (func, done) { 32 | var key = func.name + '*' + max 33 | var count = -1 34 | 35 | // true by default 36 | if (opts.color !== false) { 37 | key = chalk[nextColor()](key) 38 | } 39 | 40 | console.time(key) 41 | end() 42 | 43 | function end () { 44 | if (++count < max) { 45 | func(end) 46 | } else { 47 | console.timeEnd(key) 48 | if (done) { 49 | done() 50 | } 51 | } 52 | } 53 | } 54 | 55 | function nextColor () { 56 | if (currentColor === colors.length) { 57 | currentColor = 0 58 | } 59 | return colors[currentColor++] 60 | } 61 | } 62 | 63 | function noop () {} 64 | 65 | module.exports = build 66 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var bench = require('./') 4 | 5 | var run = bench([ 6 | function benchSetTimeout (done) { 7 | setTimeout(done, 0) 8 | }, 9 | function benchSetImmediate (done) { 10 | setImmediate(done) 11 | }, 12 | function benchNextTick (done) { 13 | process.nextTick(done) 14 | } 15 | ], 1000) 16 | 17 | run(run) 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fastbench", 3 | "version": "1.0.0", 4 | "description": "the simplest benchmark you can run on node", 5 | "main": "bench.js", 6 | "scripts": { 7 | "lint": "standard", 8 | "test": "tape test.js | faucet" 9 | }, 10 | "pre-commit": [ 11 | "lint", 12 | "test" 13 | ], 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/mcollina/fastbench.git" 17 | }, 18 | "keywords": [ 19 | "benchmark", 20 | "fast" 21 | ], 22 | "author": "Matteo Collina ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/mcollina/fastbench/issues" 26 | }, 27 | "homepage": "https://github.com/mcollina/fastbench#readme", 28 | "devDependencies": { 29 | "faucet": "0.0.1", 30 | "pre-commit": "^1.0.10", 31 | "proxyquire": "^1.6.0", 32 | "standard": "^4.5.2", 33 | "tape": "^4.0.0" 34 | }, 35 | "dependencies": { 36 | "chalk": "^1.1.0", 37 | "fastseries": "^1.3.1" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var test = require('tape') 4 | var bench = require('./') 5 | var proxyquire = require('proxyquire') 6 | var chalk = require('chalk') 7 | 8 | test('calls multiple functions', function (t) { 9 | t.plan(42 * 2) 10 | 11 | var run = bench([ 12 | function first (done) { 13 | t.pass('first called') 14 | setImmediate(done) 15 | }, 16 | function second (done) { 17 | t.pass('second called') 18 | setImmediate(done) 19 | } 20 | ], 42) 21 | 22 | run() 23 | }) 24 | 25 | test('supports a callback', function (t) { 26 | t.plan(1) 27 | 28 | var run = bench([ 29 | function first (done) { 30 | setImmediate(done) 31 | } 32 | ], 42) 33 | 34 | run(t.pass.bind(t)) 35 | }) 36 | 37 | test('does two rounds', function (t) { 38 | t.plan(42 * 2 * 2) 39 | 40 | var run = bench([ 41 | function first (done) { 42 | t.pass('first called') 43 | setImmediate(done) 44 | }, 45 | function second (done) { 46 | t.pass('second called') 47 | setImmediate(done) 48 | } 49 | ], 42) 50 | 51 | run(run) 52 | }) 53 | 54 | test('supports an option object', function (t) { 55 | t.plan(42 * 2) 56 | 57 | var run = bench([ 58 | function first (done) { 59 | t.pass('first called') 60 | setImmediate(done) 61 | }, 62 | function second (done) { 63 | t.pass('second called') 64 | setImmediate(done) 65 | } 66 | ], { iterations: 42 }) 67 | 68 | run() 69 | }) 70 | 71 | test('has color', function (t) { 72 | var chalkEnabled = chalk.enabled 73 | chalk.enabled = true 74 | 75 | var bench = proxyquire('./', { 76 | console: { 77 | time: function (key) { 78 | t.ok(chalk.hasColor(key), 'has color') 79 | }, 80 | timeEnd: function (key) { 81 | t.ok(chalk.hasColor(key), 'has color') 82 | } 83 | } 84 | }) 85 | 86 | t.plan(2) 87 | 88 | var run = bench([ 89 | function first (done) { 90 | setImmediate(done) 91 | } 92 | ], { iterations: 42 }) 93 | 94 | run(function () { 95 | chalk.enabled = chalkEnabled 96 | }) 97 | }) 98 | 99 | test('disable color', function (t) { 100 | var bench = proxyquire('./', { 101 | console: { 102 | time: function (key) { 103 | t.notOk(chalk.hasColor(key), 'has no color') 104 | }, 105 | timeEnd: function (key) { 106 | t.notOk(chalk.hasColor(key), 'has no color') 107 | } 108 | } 109 | }) 110 | 111 | t.plan(2) 112 | 113 | var run = bench([ 114 | function first (done) { 115 | setImmediate(done) 116 | } 117 | ], { 118 | iterations: 42, 119 | color: false 120 | }) 121 | 122 | run() 123 | }) 124 | --------------------------------------------------------------------------------