Loading video player…

Watch One at a Time Versus All Together

Your routine starts every device at once with .map() and waits for them with Promise.all(), and it works. But how much time did that save? In this video, you watch the Lindqvists’ morning routine run twice on one clock, once one device at a time and once with all four devices started together.

This morning the blinds work again, so every device succeeds. The delays are a scaled-down toy. The lights take one second, the blinds two, the heating three, and the kettle six.

Key Points

  • One at a time, the routine takes about the sum of the delays. A for...of loop with one await in its body is the same one-at-a-time shape as four awaits in a row, since it doesn’t call the next device until the one before it is done. Each device starts where the last one ended, like a staircase, so the routine takes 1 + 6 + 2 + 3 = 12 seconds.
  • All together, the routine takes only about the slowest delay. .map() calls all four device functions without waiting for any of them, and each call hands back a pending promise straight away. The await on Promise.all() then pauses your code until every promise is fulfilled. All four start at zero and finish in order of their delays, so the routine is done when the kettle boils, at six seconds.
  • The waits overlap because all four devices start before the await. The .map() call starts all four timers before your code reaches the await, so they count down in Node at the same time.
  • Taking out a fast device changes the sum, not the slowest. Without the lights, the rule predicts that one at a time drops from twelve seconds to eleven. All together still takes six, because the kettle is still the slowest device.
  • Starting the four devices at once saves the Lindqvists six seconds.
  • The timelines are idealized. A timer waits about its delay, so a real run comes out a little different from these round numbers, usually a little longer.

The Code

The stand-in devices, with the blinds working again:

Language: JavaScript Filename: devices.mjs
import { setTimeout as delay } from "node:timers/promises";

export async function turnOnLights() {
  await delay(1000);
  return "Lights on";
}

export async function openBlinds() {
  await delay(2000);
  return "Blinds open";
}

export async function warmUpHeating() {
  await delay(3000);
  return "Heating on";
}

export async function boilKettle() {
  await delay(6000);
  return "Kettle boiled";
}

One device at a time, each await pauses the for...of loop until that device is done, so the next device isn’t called until then. The seconds() helper returns how long it’s been since the start:

Language: JavaScript Filename: one_at_a_time.mjs
import {
  turnOnLights,
  boilKettle,
  openBlinds,
  warmUpHeating,
} from "./devices.mjs";

const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);

const routine = [turnOnLights, boilKettle, openBlinds, warmUpHeating];

for (const startDevice of routine) {
  const status = await startDevice();
  console.log(`${seconds()} s: ${status}`);
}

Run it, and the routine takes twelve seconds:

Language: Shell
$ node one_at_a_time.mjs
1.0 s: Lights on
7.0 s: Kettle boiled
9.0 s: Blinds open
12.0 s: Heating on

All four devices at once, started by .map() and awaited with Promise.all(), with the same helper:

Language: JavaScript Filename: all_together.mjs
import {
  turnOnLights,
  boilKettle,
  openBlinds,
  warmUpHeating,
} from "./devices.mjs";

const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);

const routine = [turnOnLights, boilKettle, openBlinds, warmUpHeating];
const running = routine.map((startDevice) => startDevice());

const statuses = await Promise.all(running);
console.log(`${seconds()} s:`, statuses);

Run it, and the routine takes six seconds, the kettle’s delay:

Language: Shell
$ node all_together.mjs
6.0 s: [ 'Lights on', 'Kettle boiled', 'Blinds open', 'Heating on' ]

To try the prediction, take the lights out of routine, so the line reads const routine = [boilKettle, openBlinds, warmUpHeating];. One at a time, the routine should take 6 + 2 + 3 = 11 seconds. All together, it should still take six, the kettle’s delay.

Next, in Time the Routine Two Ways, you’ll run the routine without the lights and check the six and eleven seconds you predicted.

00:00 Your routine now starts every device at once. The .map() call starts all four, and awaiting Promise.all() waits until every status is back.

00:10 It works. But how much time did that save? In this video, you’ll watch the routine run one device at a time and then all together, on one clock. This morning, the blinds are working again, so openBlinds() returns "Blinds open" after two seconds.

00:28 The other delays haven’t changed. The lights take one second, the heating three, and the kettle six. These delays are a scaled-down toy, since a real kettle takes minutes.

00:41 First, the one-at-a-time shape you started with. Your four awaits in a row are now a loop with one await, in its own file. For each device in the routine, the loop calls the device and awaits it.

00:54 It doesn’t call the next device until that await is done. The seconds() helper you’ve met before returns how long it’s been since the start. Run it, and watch the clock.

01:06 The lights start at zero and finish at one second. Only then does the kettle start. It boils at seven. The blinds start at seven and open at nine. The heating starts last and finishes at twelve.

01:23 Each device starts where the one before it ended, so the bars form a staircase. The routine takes about the sum of the four delays. One plus six plus two plus three makes twelve seconds.

01:37 Now the version you wrote, with the same helper added. The .map() call starts all four devices, without waiting. Each call hands back a pending promise straight away.

01:49 Only then does your code await Promise.all(). The await pauses your code until every promise is fulfilled.

01:58 Run it, and all four bars start at zero. The devices finish in order of their delays: the lights at one second, the blinds at two, the heating at three. The kettle is still boiling, so the await is still waiting.

02:13 At six seconds, the kettle boils, and the statuses print.

02:18 The .map() call started all four timers before your code reached the await, so all four count down in Node at the same time. The bars all start at zero, side by side, so the waits overlap.

02:31 The routine takes only about as long as the slowest device, the kettle’s six seconds.

02:38 Say the morning is bright, so Saga takes the lights out of the routine. Three devices are left: the kettle, the blinds and the heating. Your turn: pause the video and predict how long the all-together version takes now.

02:58 Worked out from the delays, not from a run, all together it should still take six seconds.

03:05 The kettle is still the slowest device, and all together, the routine waits only for the slowest. One at a time, it should drop from twelve seconds to eleven.

03:15 The lights’ one second comes off the sum, but it never counted toward the slowest.

03:22 So here’s the rule: one at a time, the routine takes about the sum of the delays. All together, it takes about the slowest delay. The bars are idealized. A timer waits about its delay, so a real run comes out a little different, usually a little longer.

03:40 With all four devices, starting them at once saves the Lindqvists six seconds. Next, in Time the Routine Two Ways, you’ll run the routine without the lights and check the six and eleven seconds you predicted.

Become a Member to join the conversation.