loopobj.js 639 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict'
  2. const Benchmark = require('benchmark')
  3. const suite = new Benchmark.Suite
  4. let obj = process.env
  5. suite
  6. .add('for in', function() {
  7. for(let i in obj) {
  8. let o = obj[i]
  9. }
  10. })
  11. .add('while --', function() {
  12. let keys = Object.keys(obj)
  13. let l = keys.length
  14. while(l--) {
  15. let o = obj[keys[l]]
  16. }
  17. })
  18. .add('while shift', function() {
  19. let keys = Object.keys(obj)
  20. let k
  21. while(k = keys.shift()) {
  22. let o = obj[k]
  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 })