1 | 'use strict'
|
2 | const Benchmark = require('benchmark')
|
3 | const suite = new Benchmark.Suite
|
4 |
|
5 | let arr = new Array(100).fill(Math.random() * 100)
|
6 |
|
7 | suite
|
8 | .add('for', function() {
|
9 | let l = arr.length
|
10 | for (let i = 0; i < l; i++) {
|
11 | let o = arr[i]
|
12 | }
|
13 | })
|
14 | .add('while --', function() {
|
15 | let l = arr.length
|
16 | while(l--) {
|
17 | let o = arr[l]
|
18 | }
|
19 | })
|
20 | .add('while ++', function() {
|
21 | let l = arr.length
|
22 | let i = -1
|
23 | while(l > ++i) {
|
24 | let o = arr[i]
|
25 | }
|
26 | })
|
27 | .on('cycle', function(event) {
|
28 | console.log(String(event.target))
|
29 | })
|
30 | .on('complete', function() {
|
31 | console.log('Fastest is ' + this.filter('fastest').map('name'))
|
32 | })
|
33 | .run({ 'async': true })
|