Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Alzheimer is a JavaScript memoization library for Node with support for streams and promises.
How can I speed up a slow function?
You might want to memoize it.
This means you put a wrapper around it that caches the function's result
so it can answer subsequent calls faster.
What does a memoization library do?
A memoization library such as Alzheimer creates this wrapper for you.
How does Alzheimer differ from other memoization libraries?
The difference from other libraries such as
memo,
memoize
and memoizer
is that Alzheimer also memoizes:
$ npm install alzheimer
The alzheimer
module exposes an object with a single function remember(func, options)
.
Call remember
with your function to retrieve a memoized version.
Memoized items can expire by setting options
to { forget: { after: 60000 } }
,
where 60000
is the number of milliseconds an item can live.
This example illustrates how Alzheimer speeds up results by memoizing a function.
// Calculates a computation-intensive hash
function calculateBigHash(number) {
for (var i = 1, result = 0; i < 20000; i++)
for (var j = 2; j < 10000; j++)
result = (result + number) * i % (j + 13);
return result;
}
// Both calculations below take equal time
calculateBigHash(5)
calculateBigHash(5)
// Now create a memoized version of the function
var remember = require("alzheimer").remember;
computeBigHash = remember(computeBigHash);
// Only the first calculation below takes some time,
// The second and third ones return immediately
calculateBigHash(5)
calculateBigHash(5)
calculateBigHash(5)
This example shows how Alzeimer can cache a stream resulting from a promise. The stream is actually played back, as the HTTP response is cached to disk. We also see that the cache can be kept fresh by demanding expiration.
var http = require("http"),
promiscuous = require("promiscuous") /* or any Promise/A+ library */,
remember = require("alzheimer").remember;
// Creates a promise to a stream of the specified HTTP resource
function request(url) {
console.log("Downloading", url);
var deferred = promiscuous.deferred();
http.get(url, deferred.resolve);
return deferred.promise;
};
// Memoize results of the `request` function for 2500 ms
var rememberedRequest = remember(request, { forget: { after: 2500 } });
// The first request will require a download
setTimeout(function () {
console.log("Request 1");
rememberedRequest("http://amazon.com/").then(function (response) {
response.on("end", console.log.bind(console, "Response 1"));
});
}, 100);
// This request won't require a download
setTimeout(function () {
console.log("Request 2");
rememberedRequest("http://amazon.com/").then(function (response) {
response.on("end", console.log.bind(console, "Response 2"));
});
}, 1000);
// This request won't require a download
setTimeout(function () {
console.log("Request 3");
rememberedRequest("http://amazon.com/").then(function (response) {
response.on("end", console.log.bind(console, "Response 3"));
});
}, 2000);
// This request will require a download again, since the cache result expired
setTimeout(function () {
console.log("Request 4");
rememberedRequest("http://amazon.com/").then(function (response) {
response.on("end", console.log.bind(console, "Response 4"));
});
}, 3000);
Copyright ©2013 Ruben Verborgh – MIT Licensed
FAQs
Advanced memoization with promise and stream support
The npm package alzheimer receives a total of 1 weekly downloads. As such, alzheimer popularity was classified as not popular.
We found that alzheimer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.