bench.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var sliced = require('./')
  2. var Bench = require('benchmark');
  3. var s = new Bench.Suite;
  4. var slice = [].slice;
  5. s.add('Array.prototype.slice.call', function () {
  6. Array.prototype.slice.call(arguments);
  7. }).add('[].slice.call', function () {
  8. [].slice.call(arguments);
  9. }).add('cached slice.call', function () {
  10. slice.call(arguments)
  11. }).add('sliced', function () {
  12. sliced(arguments)
  13. }).on('cycle', function (evt) {
  14. console.log(String(evt.target));
  15. }).on('complete', function () {
  16. console.log('fastest is %s', this.filter('fastest').pluck('name'));
  17. })
  18. .run();
  19. var s = new Bench.Suite;
  20. s.add('Array.prototype.slice.call(arguments, 1)', function () {
  21. Array.prototype.slice.call(arguments, 1);
  22. }).add('[].slice.call(arguments, 1)', function () {
  23. [].slice.call(arguments, 1);
  24. }).add('cached slice.call(arguments, 1)', function () {
  25. slice.call(arguments, 1)
  26. }).add('sliced(arguments, 1)', function () {
  27. sliced(arguments, 1)
  28. }).on('cycle', function (evt) {
  29. console.log(String(evt.target));
  30. }).on('complete', function () {
  31. console.log('fastest is %s', this.filter('fastest').pluck('name'));
  32. })
  33. .run();
  34. var s = new Bench.Suite;
  35. s.add('Array.prototype.slice.call(arguments, -1)', function () {
  36. Array.prototype.slice.call(arguments, -1);
  37. }).add('[].slice.call(arguments, -1)', function () {
  38. [].slice.call(arguments, -1);
  39. }).add('cached slice.call(arguments, -1)', function () {
  40. slice.call(arguments, -1)
  41. }).add('sliced(arguments, -1)', function () {
  42. sliced(arguments, -1)
  43. }).on('cycle', function (evt) {
  44. console.log(String(evt.target));
  45. }).on('complete', function () {
  46. console.log('fastest is %s', this.filter('fastest').pluck('name'));
  47. })
  48. .run();
  49. var s = new Bench.Suite;
  50. s.add('Array.prototype.slice.call(arguments, -2, -10)', function () {
  51. Array.prototype.slice.call(arguments, -2, -10);
  52. }).add('[].slice.call(arguments, -2, -10)', function () {
  53. [].slice.call(arguments, -2, -10);
  54. }).add('cached slice.call(arguments, -2, -10)', function () {
  55. slice.call(arguments, -2, -10)
  56. }).add('sliced(arguments, -2, -10)', function () {
  57. sliced(arguments, -2, -10)
  58. }).on('cycle', function (evt) {
  59. console.log(String(evt.target));
  60. }).on('complete', function () {
  61. console.log('fastest is %s', this.filter('fastest').pluck('name'));
  62. })
  63. .run();
  64. var s = new Bench.Suite;
  65. s.add('Array.prototype.slice.call(arguments, -2, -1)', function () {
  66. Array.prototype.slice.call(arguments, -2, -1);
  67. }).add('[].slice.call(arguments, -2, -1)', function () {
  68. [].slice.call(arguments, -2, -1);
  69. }).add('cached slice.call(arguments, -2, -1)', function () {
  70. slice.call(arguments, -2, -1)
  71. }).add('sliced(arguments, -2, -1)', function () {
  72. sliced(arguments, -2, -1)
  73. }).on('cycle', function (evt) {
  74. console.log(String(evt.target));
  75. }).on('complete', function () {
  76. console.log('fastest is %s', this.filter('fastest').pluck('name'));
  77. })
  78. .run();
  79. /**
  80. * Output:
  81. *
  82. * Array.prototype.slice.call x 1,289,592 ops/sec ±2.88% (87 runs sampled)
  83. * [].slice.call x 1,345,451 ops/sec ±1.68% (97 runs sampled)
  84. * cached slice.call x 10,719,886 ops/sec ±1.04% (99 runs sampled)
  85. * sliced x 15,809,545 ops/sec ±1.46% (93 runs sampled)
  86. * fastest is sliced
  87. *
  88. */