| name | high-perf-data-algorithms |
| description | High-performance data processing patterns from 5 repos. O(n log n) array algorithms, lazy evaluation chains, statistical computation on raw data, streaming JSON parsing for large files, and concurrent async queue management. Sources: d3/d3-array, lodash/lodash, simple-statistics/simple-statistics, vitorperes/json-stream, sindresorhus/p-queue. |
/high-perf-data-algorithms
When to Use
- Processing arrays of 100k+ items (sort, bin, group, roll-up)
- Chaining multiple transforms without intermediate array allocations
- Computing statistics (mean, stddev, regression) without a heavy lib
- Parsing JSON files > 100MB without loading them into RAM
- Throttling concurrent API calls / DB queries to avoid overload
Do NOT use for
- Arrays < 1000 items (readability > optimization)
- Streaming non-JSON formats (use line-reader or csv-parse instead)
Array Algorithms (d3-array)
import { bin, rollup, group, extent, bisectLeft, sort } from 'd3-array'
const bins = bin()
.value(d => d.score)
.thresholds(20)
(data)
const byRegion = rollup(
sales,
rows => rows.reduce((s, r) => s + r.revenue, 0),
d => d.region,
d => d.quarter,
)
const idx = bisectLeft(sortedScores, targetScore)
Lazy Evaluation Chain (lodash)
import _ from 'lodash'
const result = data.filter(isActive).map(toSummary).slice(0, 100)
const result = _(data)
.filter(isActive)
.map(toSummary)
.take(100)
.value()
Statistics on Raw Data (simple-statistics)
import {
mean, median, standardDeviation,
linearRegression, linearRegressionLine,
quantile, interquartileRange,
} from 'simple-statistics'
const values = [12, 45, 67, 23, 89, 34, 56]
console.log(mean(values))
console.log(standardDeviation(values))
console.log(quantile(values, 0.75))
console.log(interquartileRange(values))
const pairs = values.map((y, x) => [x, y])
const { m, b } = linearRegression(pairs)
const predict = linearRegressionLine({ m, b })
predict(10)
Streaming JSON Parser (JSONStream)
import JSONStream from 'JSONStream'
import fs from 'fs'
fs.createReadStream('data/users-1gb.json')
.pipe(JSONStream.parse('users.*'))
.on('data', user => {
processUser(user)
})
.on('end', () => console.log('done'))
fs.createReadStream('events.ndjson')
.pipe(JSONStream.parse())
.on('data', event => queue.add(() => ingest(event)))
Concurrent Async Queue (p-queue)
import PQueue from 'p-queue'
const queue = new PQueue({ concurrency: 5 })
const rateLimited = new PQueue({ concurrency: 10, intervalCap: 10, interval: 1000 })
const results = await Promise.all(
records.map(rec => queue.add(() => fetchEnrichedData(rec)))
)
await queue.add(() => criticalSync(), { priority: 10 })
await queue.add(() => backgroundBackfill(), { priority: 1 })
queue.on('active', () => console.log(`Queue size: , pending: `))
Complexity Quick Reference
d3 bin() O(n log n) — sorts before binning
d3 rollup() O(n) — single pass
d3 bisectLeft() O(log n) — binary search (array must be sorted)
lodash lazy chain O(n/k) — early exit after take(k)
simple-statistics O(n) — most; median O(n log n)
JSONStream parse O(1) space — streaming, no full parse
p-queue add() O(1) — constant enqueue
Anti-Fake-Pass Checklist
❌ Calling .value() inside a .map() callback (cancels lazy eval)
❌ JSONStream on NDJSON using 'users.*' path (wrong parser for flat streams)
❌ p-queue without concurrency cap (default = Infinity = thundering herd)
❌ mean() on unsorted data expecting sorted output (it's unordered)
❌ Rolling statistics inside a nested loop instead of single-pass rollup
❌ Binary search (bisectLeft) on unsorted array (silently wrong result)