looparr.js 642 B

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