fibonacci.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* global console */
  2. /* eslint no-console: 0, id-length: 0 */
  3. "use strict";
  4. // Simple benchmark for very simple memoization case (fibonacci series)
  5. // To run it, do following in memoizee package path:
  6. //
  7. // $ npm install underscore lodash lru-cache secondary-cache
  8. // $ node benchmark/fibonacci.js
  9. var forEach = require("es5-ext/object/for-each")
  10. , pad = require("es5-ext/string/#/pad")
  11. , memoizee = require("..")
  12. , underscore = require("underscore").memoize
  13. , lodash = require("lodash").memoize
  14. , lruCache = require("lru-cache")
  15. , lruSecondary = require("secondary-cache/lib/lru-cache");
  16. var now = Date.now
  17. , time
  18. , getFib
  19. , lru
  20. , memo
  21. , total
  22. , index = 3000
  23. , count = 10
  24. , i
  25. , lruMax = 1000
  26. , data = {}
  27. , lruObj;
  28. getFib = function (memoize, opts) {
  29. var fib = memoize(function (x) {
  30. return x < 2 ? 1 : fib(x - 1) + fib(x - 2);
  31. }, opts);
  32. return fib;
  33. };
  34. lru = function (x) {
  35. var value = lruObj.get(x);
  36. if (value === undefined) {
  37. value = x < 2 ? 1 : lru(x - 1) + lru(x - 2);
  38. lruObj.set(x, value);
  39. }
  40. return value;
  41. };
  42. console.log("Fibonacci", index, "x" + count + ":\n");
  43. total = 0;
  44. i = count;
  45. while (i--) {
  46. memo = getFib(memoizee);
  47. time = now();
  48. memo(index);
  49. total += now() - time;
  50. }
  51. data["Memoizee (object mode)"] = total;
  52. total = 0;
  53. i = count;
  54. while (i--) {
  55. memo = getFib(memoizee, { primitive: true });
  56. time = now();
  57. memo(index);
  58. total += now() - time;
  59. }
  60. data["Memoizee (primitive mode)"] = total;
  61. total = 0;
  62. i = count;
  63. while (i--) {
  64. memo = getFib(underscore);
  65. time = now();
  66. memo(index);
  67. total += now() - time;
  68. }
  69. data.Underscore = total;
  70. total = 0;
  71. i = count;
  72. while (i--) {
  73. memo = getFib(lodash);
  74. time = now();
  75. memo(index);
  76. total += now() - time;
  77. }
  78. data["Lo-dash"] = total;
  79. total = 0;
  80. i = count;
  81. while (i--) {
  82. memo = getFib(memoizee, { primitive: true, max: lruMax });
  83. time = now();
  84. memo(index);
  85. total += now() - time;
  86. }
  87. data["Memoizee (primitive mode) LRU (max: 1000)"] = total;
  88. total = 0;
  89. i = count;
  90. while (i--) {
  91. memo = getFib(memoizee, { max: lruMax });
  92. time = now();
  93. memo(index);
  94. total += now() - time;
  95. }
  96. data["Memoizee (object mode) LRU (max: 1000)"] = total;
  97. total = 0;
  98. i = count;
  99. while (i--) {
  100. lruObj = lruCache({ max: lruMax });
  101. time = now();
  102. lru(index);
  103. total += now() - time;
  104. }
  105. data["lru-cache LRU (max: 1000)"] = total;
  106. total = 0;
  107. i = count;
  108. while (i--) {
  109. lruObj = lruSecondary(lruMax);
  110. time = now();
  111. lru(index);
  112. total += now() - time;
  113. }
  114. data["secondary-cache LRU (max: 1000)"] = total;
  115. forEach(
  116. data,
  117. function (value, name, obj, currentIndex) {
  118. console.log(currentIndex + 1 + ":", pad.call(value, " ", 5) + "ms ", name);
  119. },
  120. null,
  121. function (a, b) {
  122. return this[a] - this[b];
  123. }
  124. );