Debian Codegraph @

memoizee/benchmark/fibonacci.js

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