]> git.ardis.lu - csvio/commitdiff
Simplify and harden tick indicator
authorArdis Lu <ardisglu@gmail.com>
Fri, 4 Jul 2025 03:45:55 +0000 (20:45 -0700)
committerArdis Lu <ardisglu@gmail.com>
Fri, 4 Jul 2025 03:45:55 +0000 (20:45 -0700)
Relying on physical timing is flaky (e.g., it will fail if executing on a slow machine).  Sending a simple `tick` increment into the task queue is more reliable.

examples/data/ex3_2-out.csv
examples/ex3_2.js

index de20589205b26f3859e995e47e5a29ab07695f9a..334d7d3806a928e4895834e31f5f55c0df24d53c 100644 (file)
@@ -1,9 +1,9 @@
 tick number,concurrent group\r
-1,a\r
-1,a\r
-1,a\r
-1,a\r
-1,a\r
-2,b\r
-2,b\r
-2,b\r
+0,a\r
+0,a\r
+0,a\r
+0,a\r
+0,a\r
+5,b\r
+5,b\r
+5,b\r
index bf341b60833f17c63d576ab8e535db28a18419a0..7f83dc6449cfb42d493d76a7bfd18c27ccff68ad 100644 (file)
@@ -7,19 +7,12 @@
 // takes very long, all row processing is blocked on the 1 transformation even if all others in the group have already
 // resolved.
 
-import { setTimeout } from 'node:timers/promises';
 import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
 
-// Returns how many 100ms increments have elapsed since this program began
-const start = performance.now();
-function tick() {
-  const ms = performance.now() - start;
-  return Math.floor(ms / 100);
-}
-
+let tick = 0;
 async function concurrent(row) {
-  await setTimeout(100); // Simulate some slow task, e.g. a network request
-  return [tick(), ...row];
+  setTimeout(() => tick++, 0); // Tick will only be incremented after the concurrent group is done
+  return [tick, ...row];
 }
 
 await new CSVReader(new URL('./data/ex3_2-in.csv', import.meta.url))
This page took 0.039622 seconds and 4 git commands to generate.