Time the Routine Two Ways

In the last video, you watched the Lindqvists’ routine run two ways. One device at a time, it took about the sum of the four delays. All together, it took about as long as the slowest device, the kettle. The video ended on a prediction. Without the lights, the all-together routine should still take about six seconds, while the one-at-a-time routine should drop from about twelve seconds to about eleven.

In this lesson, you’ll check that prediction with a stopwatch of your own and measure what happens when the routine starts many more jobs at once. Then Saga adds a step that has to wait for the kettle, and you’ll keep that step in order without making every other device wait. Start with the prediction.

Check the Prediction With console.time()

The video drew its bars from the delays, so its prediction is still a claim until a run backs it up. To measure it, you can use the stopwatch built into the console object instead of writing a seconds() helper with Date.now() into every file.

console.time() starts a timer under a label you choose. console.timeEnd() with the same label stops that timer and prints the label and the time since it started. Each timer in a program needs its own label.

Work in your morning folder, using the version of devices.mjs from the last video, in which the blinds work again. If you’re not sure which version you have, then replace it with this one:

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";
}

The morning is bright, so the lights stay out of routine. Save no_lights.mjs, which runs the other three devices with a for...of loop that awaits each device, then with .map() and Promise.all(), and wraps each shape in its own timer:

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

const routine = [boilKettle, openBlinds, warmUpHeating];

console.time("one at a time");
for (const startDevice of routine) {
  await startDevice();
}
console.timeEnd("one at a time");

console.time("all together");
await Promise.all(routine.map((startDevice) => startDevice()));
console.timeEnd("all together");

Nothing logs the statuses this time, because the timings are the result.

Watch out: even though the device functions are async, the loop doesn’t run them at the same time. Each await pauses the loop until that device is done, so the loop doesn’t even start the blinds until the kettle has boiled.

Run node no_lights.mjs:

Language: Program Output
one at a time: 11.012s
all together: 6.004s

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.