+1
-1
| { | ||
| "name": "libtuple", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "author": "Sean Morris", | ||
@@ -5,0 +5,0 @@ "description": "Memory-efficient tuple implementation.", |
+1
-1
@@ -81,3 +81,3 @@ # libtuple.js | ||
| * ~~The library cannot tell the difference between `null` and `undefined`~~. | ||
| * Purely scalar-based tuples are represented by strings, not objects. | ||
| * ~~Purely scalar-based tuples are represented by strings, not objects.~~ | ||
@@ -84,0 +84,0 @@ ## Building |
+30
-15
| const refTree = new WeakMap; | ||
| const scalarMap = new Map; | ||
| const terminator = Object.create(null); | ||
| const baseTuple = Object.create(null); | ||
| baseTuple.toString = () => '[Object Tuple]'; | ||
| baseTuple[Symbol.toStringTag] = 'Tuple'; | ||
| baseTuple.toString = Object.prototype.toString; | ||
| Object.freeze(terminator); | ||
| Object.freeze(baseTuple); | ||
| module.exports = function Tuple(...args) | ||
| { | ||
| const registry = new FinalizationRegistry(held => scalarMap.delete(held)); | ||
| module.exports = function Tuple(...args){ | ||
| if(new.target) | ||
@@ -16,7 +20,2 @@ { | ||
| if(!args.length) | ||
| { | ||
| return '[]'; | ||
| } | ||
| let prefix = null; | ||
@@ -46,9 +45,8 @@ let mode = null; | ||
| } | ||
| else if(canMap) | ||
| { | ||
| prefix = JSON.stringify(part.map(p => `${typeof p}::${p}`)) | ||
| } | ||
| else | ||
| { | ||
| if(canMap) | ||
| { | ||
| prefix = JSON.stringify(part.map(p => `${typeof p}::${p}`)) | ||
| } | ||
| part = [arg]; | ||
@@ -71,3 +69,3 @@ } | ||
| map = maps.map; | ||
| if(prefix) | ||
@@ -95,3 +93,16 @@ { | ||
| { | ||
| return part; | ||
| const result = Object.create(baseTuple); | ||
| Object.assign(result, {length:args.length, ...args}); | ||
| Object.freeze(result); | ||
| if(!scalarMap.has(part)) | ||
| { | ||
| registry.register(result, part); | ||
| scalarMap.set(part, new WeakRef(result)); | ||
| return result; | ||
| } | ||
| else | ||
| { | ||
| return scalarMap.get(part).deref(); | ||
| } | ||
| } | ||
@@ -102,2 +113,3 @@ | ||
| const result = Object.create(baseTuple); | ||
| Object.assign(result, {length:args.length, ...args}); | ||
| Object.freeze(result); | ||
@@ -114,2 +126,3 @@ | ||
| const result = Object.create(baseTuple); | ||
| Object.assign(result, {length:args.length, ...args}); | ||
| Object.freeze(result); | ||
@@ -122,1 +135,3 @@ | ||
| } | ||
| Object.defineProperty(module.exports, 'scalarsCached', {get: () => scalarMap.size}); |
7111
9.57%103
14.44%