Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

memize

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memize - npm Package Compare versions

Comparing version
1.0.1
to
1.0.2
+54
dist/memize.fifo.js
var memize = (function () {
'use strict';
var fifo = function memize( fn, options ) {
var cache = [],
maxSize;
if ( options && options.maxSize > 0 ) {
maxSize = options.maxSize;
}
function memoized( /* ...args */ ) {
searchCache: for ( var i = 0; i < cache.length; i++ ) {
// Check whether cached arguments match current invocation
for ( var j = 0; j < arguments.length; j++ ) {
if ( cache[ i ][ 0 ][ j ] !== arguments[ j ] ) {
continue searchCache;
}
// At this point, a assume a match is found and return
return cache[ i ][ 1 ];
}
}
// No cached value found. Continue to insertion phase:
// Create a copy of arguments (avoid leaking deoptimization)
var args = [];
for ( i = 0; i < arguments.length; i++ ) {
args.push( arguments[ i ] );
}
// Generate the result from original function
var result = fn.apply( null, args );
cache.push( [ args, result ] );
// Trim if we're reached max size and are pending cache insertion
if ( cache.length > maxSize ) {
cache = cache.slice( 1 );
}
return result;
}
memoized.clear = function() {
cache = [];
};
return memoized;
};
return fifo;
}());
var memize=function(){"use strict";return function(n,r){function e(){n:for(var r=0;r<u.length;r++)for(var e=0;e<arguments.length;e++){if(u[r][0][e]!==arguments[e])continue n;return u[r][1]}var i=[];for(r=0;r<arguments.length;r++)i.push(arguments[r]);var a=n.apply(null,i);return u.push([i,a]),u.length>t&&(u=u.slice(1)),a}var t,u=[];return r&&r.maxSize>0&&(t=r.maxSize),e.clear=function(){u=[]},e}}();
+4
-0

@@ -0,1 +1,5 @@

#### v1.0.2 (2017-08-24)
- Fix: Resolve infinite loop which can occur due to lingering references in recalling from previous cache
#### v1.0.1 (2017-08-09)

@@ -2,0 +6,0 @@

@@ -28,2 +28,7 @@ var memize = (function () {

// Shift tail to previous
if ( node === tail ) {
tail = node.prev;
}
// Surface matched node to head if not already

@@ -33,2 +38,5 @@ if ( node !== head ) {

node.next = head;
node.prev = null;
head.prev = node;
head = node;
}

@@ -35,0 +43,0 @@

+1
-1

@@ -1,1 +0,1 @@

var memize=function(){"use strict";return function(n,r){function u(){var r,u,i=e,o=arguments.length;n:for(;i;){for(u=0;u<o;u++)if(i.n[u]!==arguments[u]){i=i.r;continue n}return i!==e&&(i.u.r=i.r,i.r=e),i.t}for(r=new Array(o),u=0;u<o;u++)r[u]=arguments[u];return i={n:r,t:n.apply(null,r)},e?(e.u=i,i.r=e):l=i,f===t?(l=l.u).r=null:f++,e=i,i.t}var t,e,l,f=0;return r&&r.e&&(t=r.e),u.clear=function(){e=null,l=null,f=0},u}}();
var memize=function(){"use strict";return function(n,r){function u(){var r,u,i=t,o=arguments.length;n:for(;i;){for(u=0;u<o;u++)if(i.n[u]!==arguments[u]){i=i.r;continue n}return i===e&&(e=i.u),i!==t&&(i.u.r=i.r,i.r=t,i.u=null,t.u=i,t=i),i.l}for(r=new Array(o),u=0;u<o;u++)r[u]=arguments[u];return i={n:r,l:n.apply(null,r)},t?(t.u=i,i.r=t):e=i,f===l?(e=e.u).r=null:f++,t=i,i.l}var l,t,e,f=0;return r&&r.t&&(l=r.t),u.clear=function(){t=null,e=null,f=0},u}}();

@@ -25,2 +25,7 @@ module.exports = function memize( fn, options ) {

// Shift tail to previous
if ( node === tail ) {
tail = node.prev;
}
// Surface matched node to head if not already

@@ -30,2 +35,5 @@ if ( node !== head ) {

node.next = head;
node.prev = null;
head.prev = node;
head = node;
}

@@ -32,0 +40,0 @@

{
"name": "memize",
"version": "1.0.1",
"version": "1.0.2",
"description": "Unabashedly-barebones memoization library with an aim toward speed",

@@ -5,0 +5,0 @@ "main": "index.js",

Memize
======
Memize is a unabashedly-barebones memoization library with an aim toward speed. By all accounts, Memize is __the fastest memoization implementation__ in JavaScript (see [benchmarks](#benchmarks), [how it works](#how-it-works)). It supports multiple arguments, including non-primitive arguments (by reference). All this weighing in at a paltry 0.27kb minified and gzipped, with no dependencies.
Memize is a unabashedly-barebones memoization library with an aim toward speed. By all accounts, Memize is __the fastest memoization implementation__ in JavaScript (see [benchmarks](#benchmarks), [how it works](#how-it-works)). It supports multiple arguments, including non-primitive arguments (by reference). All this weighing in at less than 0.3kb minified and gzipped, with no dependencies.

@@ -6,0 +6,0 @@ ## Example

var memize = (function () {
'use strict';
var index = function memize( fn, options ) {
var size = 0,
maxSize, head, tail;
if ( options && options.maxSize ) {
maxSize = options.maxSize;
}
function memoized( /* ...args */ ) {
var node = head,
len = arguments.length,
args, i;
searchCache: while ( node ) {
// Check whether node arguments match arguments
for ( i = 0; i < len; i++ ) {
if ( node.args[ i ] !== arguments[ i ] ) {
node = node.next;
continue searchCache;
}
}
// At this point we can assume we've found a match
// Surface matched node to head if not already
if ( node !== head ) {
node.prev.next = node.next;
node.next = head;
}
// Return immediately
return node.val;
}
// No cached value found. Continue to insertion phase:
// Create a copy of arguments (avoid leaking deoptimization)
args = new Array( len );
for ( i = 0; i < len; i++ ) {
args[ i ] = arguments[ i ];
}
node = {
args: args,
// Generate the result from original function
val: fn.apply( null, args )
};
// Don't need to check whether node is already head, since it would
// have been returned above already if it was
// Shift existing head down list
if ( head ) {
head.prev = node;
node.next = head;
} else {
// If no head, follows that there's no tail (at initial or reset)
tail = node;
}
// Trim tail if we're reached max size and are pending cache insertion
if ( size === maxSize ) {
tail = tail.prev;
tail.next = null;
} else {
size++;
}
head = node;
return node.val;
}
memoized.clear = function() {
head = null;
tail = null;
size = 0;
};
return memoized;
};
return index;
}());