Collections vs JS Arrays

Kotlin Collections Versus JavaScript Arrays and Objects

Here is the pipeline of Collection Pipelines in JavaScript, run with Node 22 on the same booknest-data.json:

The same catalog pipeline with JavaScript array methods (pipeline.mjs)JavaScript
import { readFileSync } from "node:fs";
const { books } = JSON.parse(readFileSync("booknest-data.json", "utf8"));
const cheap = books.filter((b) => b.inStock && b.price < 20)
  .toSorted((a, b) => a.price - b.price).map((b) => b.title);
console.log(cheap.join(" | "));
const bySize = Object.groupBy(books, (b) => (b.pages > 300 ? "long" : "short"));
console.log(Object.entries(bySize).map(([k, v]) => `${k}: ${v.map((b) => b.id)}`));
console.log(books.reduce((acc, b) => acc + (b.inStock ? b.price : 0), 0));
const lazy = books.values().filter((b) => b.price < 20).map((b) => b.id).take(2);
console.log(lazy.toArray(), [1, 2] === [1, 2], new Set([[1], [1]]).size);
Output
The Quiet Harbor | The Clockmaker's Paradox | Small Steps to Big Summits
[ 'long: 1,2,5', 'short: 3,4,6' ]
110.74000000000001
[ 1, 4 ] false 2

The results match; JavaScript prints the unrounded double 110.74000000000001 that Kotlin's "%.2f" hid. When you port code, remember that List<T> is read-only at compile time, whereas Object.freeze() acts only at run time; that sort() mutates, so ES2023's toSorted() is the true sortedBy; and that JavaScript compares arrays by reference, so a Set keeps both [1] arrays, while Kotlin compares lists and data classes by value. The ES2025 iterator helpers (values().filter().take()) are lazy, like sequences.