
Security News
RubyGems Adds Cooldown Feature to Bundler for Newly Published Gems
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.
Memory-efficient tuple implementation in 2.5kB
$ npm install libtuple
import Tuple from 'libtuple';
import Tuple from 'libtuple';
Pass a list of values to the Tuple() function:
const tuple123 = Tuple(1, 2, 3);
This value will be strictly equivalent to any tuple generated with the same values:
tuple123 === Tuple(1, 2, 3); // true
This is true for tuples with objects as well:
const a = {};
const b = [];
const c = new Date;
console.log( Tuple(a, b, c, 1, 2, 3) === Tuple(a, b, c, 1, 2, 3) ); //true
Watch out for the following however, object references can be tricky. In this example, each [] represents its own, unique object, so the following returns false:
Tuple( [] ) === Tuple( [] ); // FALSE!!!
Use the same object reference to get the same tuple:
const a = [];
Tuple( a ) === Tuple( a ); // true :)
Tuples can be members of of other tuples. This works as expected:
console.log( Tuple(Tuple(1, 2), Tuple(3, 4)) === Tuple(Tuple(1, 2), Tuple(3, 4)) );
// true
You cannot add, remove or modify any property on a tuple.
const tuple = Tuple('a', 'b', 'c');
tuple[0] = 'NEW VALUE';
console.log( tuple[0] ); // 'a'
You can access properties like [0], [1], and .length on a tuple, but they are not arrays. You can get equivalent array values quite easily with Array.from():
const tuple = Tuple('a', 'b', 'c');
console.log( tuple[1] ) // 'b'
Array.from(tuple).map(t => console.log(t));
// 'a'
// 'b'
// 'c'
A tuple is a type represented by a sequence of values. Unlike arrays, where [1,2] !== [1,2], since, although they hold the same values, the actual object references are different. Tuples give you Tuple(1,2) === Tuple(1,2).
For a sequence of primitives, this is trivial. Simply run JSON.stringify on the list of values and you've got a unique scalar that you can compare against others, and the object-reference problem is gone. Once you add objects to the mix, however, things can get complicated.
Stringifying objects won't work, since given almost any stringification mechanism, two completely disparate objects can be coerced to resolve the same value. That only leaves us with bean-counting. If we keep track of which objects and scalars we've seen, we can use the unique value of the object reference itself to construct a path through a tree of Maps where the leaves are the ultimate scalar value of the tuple. But in that case we'll use memory to hold objects and scalars in memory long after their tuples are useful. It seems we're backed into a corner here.
We could use trees of WeakMaps instead, however this case would only allow us to use objects, since scalars cannot be used as keys to a WeakMap. We'd end up with two disparate mechanisms, one for lists of only scalars, and one for lists of only objects. We just can't win here!
And that's where prefix-trees come in. Before constructing a tree of WeakMaps, the function will group all neighboring scalars into singular values. This will then leave us with a list of objects interspersed by singular scalars. Each scalar is then considered the prefix of the next object. When constructing or traversing the tree, first we come upon a node representing the object, then its prefix, then the next object in the chain. If the first (or any) object has no scalar prefix, we simply move directly to the next object. If the list ends in a scalar, simply add a terminator object reference as a key to the leaf, which holds the actual tuple object.
Organizing the hierarchy with the scalar prefixes after the objects allows us to exploit the WeakMap's garbage collection behavior. Once the object keys are GC'ed, so are the entries of the WeakMap. Holding a key here does not prevent objects from being GC'ed, so the branches of the internal tuple tree only stay in-memory as long as the objects they're comprised of.
Symbols cannot participate in tuples.Run npm run test or node --test test.mjs in the terminal.
FAQs
Memory-efficient tuple implementation.
The npm package libtuple receives a total of 24 weekly downloads. As such, libtuple popularity was classified as not popular.
We found that libtuple demonstrated a healthy version release cadence and project activity because the last version was released less than 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
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.

Security News
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.

Security News
Federal audit finds NIST lacked a plan to clear the NVD backlog, wasted funds on duplicate work, and delayed use of CISA data.