collections
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -0,2 +1,5 @@ | ||
## v1.2.3 | ||
- Dict Optimization to remove the need to mangle/unmangle keys. This minimize the amount of string creation and therefore garbage collection | ||
## v1.2.2 | ||
@@ -3,0 +6,0 @@ |
145
dict.js
@@ -17,3 +17,3 @@ "use strict"; | ||
this.getDefault = getDefault; | ||
this.store = {}; | ||
this.store = Object.create(null); | ||
this.length = 0; | ||
@@ -25,13 +25,2 @@ this.addEach(values); | ||
function mangle(key) { | ||
// Use "$" as the mangle prefix so dictionaries of valid identifiers can | ||
// take advantage of optimizations for objects containing only valid | ||
// identifiers. I have not verified that this makes a difference. | ||
return "$" + key; | ||
} | ||
function unmangle(mangled) { | ||
return mangled.slice(1); | ||
} | ||
Object.addEach(Dict.prototype, GenericCollection.prototype); | ||
@@ -51,12 +40,37 @@ Object.addEach(Dict.prototype, GenericMap.prototype); | ||
Object.defineProperty(Dict.prototype,"$__proto__",{writable:true}); | ||
Object.defineProperty(Dict.prototype,"_hasProto",{ | ||
get:function() { | ||
return this.hasOwnProperty("$__proto__") && typeof this._protoValue !== "undefined"; | ||
} | ||
}); | ||
Object.defineProperty(Dict.prototype,"_protoValue",{ | ||
get:function() { | ||
return this["$__proto__"]; | ||
}, | ||
set: function(value) { | ||
this["$__proto__"] = value; | ||
} | ||
}); | ||
Dict.prototype.get = function (key, defaultValue) { | ||
this.assertString(key); | ||
var mangled = mangle(key); | ||
if (mangled in this.store) { | ||
return this.store[mangled]; | ||
} else if (arguments.length > 1) { | ||
return defaultValue; | ||
} else { | ||
return this.getDefault(key); | ||
if (key === "__proto__") { | ||
if (this._hasProto) { | ||
return this._protoValue; | ||
} else if (arguments.length > 1) { | ||
return defaultValue; | ||
} else { | ||
return this.getDefault(key); | ||
} | ||
} | ||
else { | ||
if (key in this.store) { | ||
return this.store[key]; | ||
} else if (arguments.length > 1) { | ||
return defaultValue; | ||
} else { | ||
return this.getDefault(key); | ||
} | ||
} | ||
}; | ||
@@ -66,8 +80,13 @@ | ||
this.assertString(key); | ||
var mangled = mangle(key); | ||
if (mangled in this.store) { // update | ||
var isProtoKey = (key === "__proto__"); | ||
if (isProtoKey ? this._hasProto : key in this.store) { // update | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this.store[mangled]); | ||
this.dispatchBeforeMapChange(key, isProtoKey ? this._protoValue : this.store[key]); | ||
} | ||
this.store[mangled] = value; | ||
isProtoKey | ||
? this._protoValue = value | ||
: this.store[key] = value; | ||
if (this.dispatchesMapChanges) { | ||
@@ -82,3 +101,7 @@ this.dispatchMapChange(key, value); | ||
this.length++; | ||
this.store[mangled] = value; | ||
isProtoKey | ||
? this._protoValue = value | ||
: this.store[key] = value; | ||
if (this.dispatchesMapChanges) { | ||
@@ -93,4 +116,3 @@ this.dispatchMapChange(key, value); | ||
this.assertString(key); | ||
var mangled = mangle(key); | ||
return mangled in this.store; | ||
return key === "__proto__" ? this._hasProto : key in this.store; | ||
}; | ||
@@ -100,26 +122,49 @@ | ||
this.assertString(key); | ||
var mangled = mangle(key); | ||
if (mangled in this.store) { | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this.store[mangled]); | ||
if (key === "__proto__") { | ||
if (this._hasProto) { | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this._protoValue); | ||
} | ||
this._protoValue = undefined; | ||
this.length--; | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchMapChange(key, undefined); | ||
} | ||
return true; | ||
} | ||
delete this.store[mangle(key)]; | ||
this.length--; | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchMapChange(key, undefined); | ||
return false; | ||
} | ||
else { | ||
if (key in this.store) { | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this.store[key]); | ||
} | ||
delete this.store[key]; | ||
this.length--; | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchMapChange(key, undefined); | ||
} | ||
return true; | ||
} | ||
return true; | ||
return false; | ||
} | ||
return false; | ||
}; | ||
Dict.prototype.clear = function () { | ||
var key, mangled; | ||
for (mangled in this.store) { | ||
key = unmangle(mangled); | ||
var key; | ||
if (this._hasProto) { | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this.store[mangled]); | ||
this.dispatchBeforeMapChange("__proto__", this._protoValue); | ||
} | ||
delete this.store[mangled]; | ||
this._protoValue = undefined; | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchMapChange("__proto__", undefined); | ||
} | ||
} | ||
for (key in this.store) { | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchBeforeMapChange(key, this.store[key]); | ||
} | ||
delete this.store[key]; | ||
if (this.dispatchesMapChanges) { | ||
this.dispatchMapChange(key, undefined); | ||
@@ -132,5 +177,9 @@ } | ||
Dict.prototype.reduce = function (callback, basis, thisp) { | ||
for (var mangled in this.store) { | ||
basis = callback.call(thisp, basis, this.store[mangled], unmangle(mangled), this); | ||
if(this._hasProto) { | ||
basis = callback.call(thisp, basis, "$__proto__", "__proto__", this); | ||
} | ||
var store = this.store; | ||
for (var key in this.store) { | ||
basis = callback.call(thisp, basis, store[key], key, this); | ||
} | ||
return basis; | ||
@@ -142,5 +191,10 @@ }; | ||
var store = this.store; | ||
return Object.keys(this.store).reduceRight(function (basis, mangled) { | ||
return callback.call(thisp, basis, store[mangled], unmangle(mangled), self); | ||
basis = Object.keys(this.store).reduceRight(function (basis, key) { | ||
return callback.call(thisp, basis, store[key], key, self); | ||
}, basis); | ||
if(this._hasProto) { | ||
return callback.call(thisp, basis, this._protoValue, "__proto__", self); | ||
} | ||
return basis; | ||
}; | ||
@@ -153,2 +207,3 @@ | ||
} | ||
return this._protoValue; | ||
}; | ||
@@ -155,0 +210,0 @@ |
{ | ||
"name": "collections", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "data structures with idiomatic JavaScript collection interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "http://www.collectionsjs.com", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
215074
5902
0