Public
Edited
Feb 10, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
experiments = [
/*{
name: "Baseline for-loop",
run: () => {
for (let i = 0; i < N; i++) {
const x1 = objs[i].x1,
y1 = cols.y1[i];
verifier(x1, y1);
}
}
},
{
name: "Baseline iterator",
run: () => {
for (const it of fakes) {
const x1 = it.x1,
y1 = it.y1;
verifier(x1, y1);
}
}
},
{
name: "Baseline iterator + destructure",
run: () => {
for (const it of fakes) {
const { x1, y1 } = it;
verifier(x1, y1);
}
}
},*/
{
name: "Baseline iterator using helper",
run: testIterator(fakes)
},
{
name: "Splat + assignment, generator",
run: testIterator(
(function* () {
const keys = Object.keys(cols);
for (let i = 0; i < N; i++) {
const row = { ...objs[i] };
for (const key of keys) {
row[key] = cols[key][i];
}
yield row;
}
})()
)
}
]
Insert cell
objs= Array.from({ length: N }, (_, n) => ({ x1: n, x2: true, x3: "hi" }))

Insert cell
cols = ({
y1: Array.from({ length: N }, (_, n) => n * 2),
y2: Array.from({ length: N }, (_, n) => false),
y3: Array.from({ length: N }, (_, n) => "bye")
})
Insert cell
fakes = Array.from({ length: N }, (_, n) => ({ x1: n, y1: n * 2 }))
Insert cell
function testIterator(it) {
return function () {
let i = 0;
for (const value of it) {
const x1 = value.x1,
y1 = value.y1;
verifier(x1, y1, i++);
}
if (i !== N) throw new Error("Not enoug iterations: " + i);
};
}
Insert cell
viewof verify = Inputs.toggle(
"Verify correctness (slower and pollutes measurements)"
)
Insert cell
verify
Insert cell
verifier = verify
? (x1, y1, i) => {
if (x1 * 2 !== y1)
throw new Error(`Verification x1*2=y1 x1=${x1} y1=${y1}`);
if (x1 !== i) throw new Error(`Verification x1=i x1=${x1} i=${i}`);
}
: () => {}
Insert cell
Insert cell
Insert cell
performance.now()
Insert cell
Insert cell
Insert cell
({
f: (function* () {
const keys = Object.keys(cols);
for (let i = 0; i < N; i++) {
const row = { ...objs[i] };
for (const key of keys) {
row[key] = cols[key][i];
}
yield row;
}
})()
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more