Files
memoizee/benchmark/fibonacci.js
Mariusz Nowak eb72d16bf6 Major reorganization and partial refactoring
- Move out main modules from `lib` folder
- Introduce `normalizer` based configurations, and convert primitive and regular handlers into thos normalizers (can be found in lib/normalizers folder). Custom normalizers can be provided at run time via `normaizer` option
- Provide `plain` module which does not load any extensions or normalizers. Any extensions that have to be used should be required upfront and normalizers should be provided directly
- Remove `method` option, instead `methods` and `methods-plan` modules are provided which generate descriptors for lazy created memoized methods
- `profile` is no longer extension. To be used it should be required directly
- Improve logic for `async` handling
- Take out `max` extensionLRU logic into external `lru-queue` package
- Remove `context` option
- Remove possibility to access original arguments when resolvers are used
- Assure expected length of memoized functions
2014-04-27 12:11:06 +02:00

114 lines
2.3 KiB
JavaScript

'use strict';
// Simple benchmark for very simple memoization case (fibonacci series)
// To run it, do following in memoizee package path:
//
// $ npm install underscore lodash lru-cache
// $ node benchmark/fibonacci.js
var forEach = require('es5-ext/object/for-each')
, pad = require('es5-ext/string/#/pad')
, memoizee = require('..')
, underscore = require('underscore').memoize
, lodash = require('lodash').memoize
, lruCache = require('lru-cache')
, now = Date.now
, time, getFib, lru, memo, total, index = 3000, count = 10, i, lruMax = 1000
, data = {}, lruObj;
getFib = function (memoize, opts) {
var fib = memoize(function (x) {
return (x < 2) ? 1 : fib(x - 1) + fib(x - 2);
}, opts);
return fib;
};
lru = function (x) {
var value = lruObj.get(x);
if (value === undefined) {
value = ((x < 2) ? 1 : lru(x - 1) + lru(x - 2));
lruObj.set(x, value);
}
return value;
};
console.log("Fibonacci", index, "x" + count + ":\n");
total = 0;
i = count;
while (i--) {
memo = getFib(memoizee);
time = now();
memo(index);
total += now() - time;
}
data["Memoizee (object mode)"] = total;
total = 0;
i = count;
while (i--) {
memo = getFib(memoizee, { primitive: true });
time = now();
memo(index);
total += now() - time;
}
data["Memoizee (primitive mode)"] = total;
total = 0;
i = count;
while (i--) {
memo = getFib(underscore);
time = now();
memo(index);
total += now() - time;
}
data["Underscore"] = total;
total = 0;
i = count;
while (i--) {
memo = getFib(lodash);
time = now();
memo(index);
total += now() - time;
}
data["Lo-dash"] = total;
total = 0;
i = count;
while (i--) {
memo = getFib(memoizee, { primitive: true, max: lruMax });
time = now();
memo(index);
total += now() - time;
}
data["Memoizee (primitive mode) LRU (max: 1000)"] = total;
total = 0;
i = count;
while (i--) {
memo = getFib(memoizee, { max: lruMax });
time = now();
memo(index);
total += now() - time;
}
data["Memoizee (object mode) LRU (max: 1000)"] = total;
total = 0;
i = count;
while (i--) {
lruObj = lruCache({ max: lruMax });
time = now();
lru(index);
total += now() - time;
}
data["lru-cache LRU (max: 1000)"] = total;
forEach(data, function (value, name, obj, index) {
console.log(index + 1 + ":", pad.call(value, " ", 5) + "ms ", name);
}, null, function (a, b) {
return this[a] - this[b];
});