Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Created January 27, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brcontainer/e5bc89280f84d811b90738d6d52c1676 to your computer and use it in GitHub Desktop.
Save brcontainer/e5bc89280f84d811b90738d6d52c1676 to your computer and use it in GitHub Desktop.
  • Para executar instale o Node.js

  • Crie um projeto/pasta e instale o Benchmark.js:

    mkdir exemplo1
    cd exemplo1
    npm i --save benchmark
    
  • Execute o comando node index.js

Deve obter algo como:

Math x 952,800,308 ops/sec ±0.21% (96 runs sampled)
while x 10,586,158 ops/sec ±0.96% (86 runs sampled)
padEnd x 11,894,867 ops/sec ±2.97% (88 runs sampled)
substring x 26,682,987 ops/sec ±1.95% (87 runs sampled)
Fastest is Math
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
// add tests
suite
.add('Math', function() {
var numero = 18;
numero * Math.pow(10, 8 - Math.log(numero) * Math.LOG10E + 1 | 0);
})
.add('while', function() {
var number = 18;
var entry = number.toString();
while (entry.length < 9) { entry += '0'; }
})
.add('padEnd', function() {
var number = 18;
number.toString().padEnd(9, '0');
})
.add('substring', function() {
var number = 18;
(number.toString() + '000000000').substring(0, 8);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment