• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

[JS] One Mistake I Made with await in a Loop

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
A while back, I built a utility script to process thousands of items with an asyncfunction. I thought slapping await inside a for() loop would do the trick. Spoiler: it didn’t!

That simple mistake taught me a big lesson about how await, for, map, and Promise.all work. As a back-end developer, I now use this trick to optimize APIs. In this post, I’ll break down what went wrong and how I fixed it with Promise.all.

Here’s the messy code I started with:


for (const item of items) {
await process(item);
}

At first, I thought I was a genius. Slapping await inside a for() Loop to process an array? Worked like a charm… until I hit 500+ items. My script crawled slower than a turtle stuck in traffic.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



The rookie mistake? I was running tasks one by one, like a single-threaded program. No parallelism, no speed. Oh my god.

The better approach was to use Promise.all


await Promise.all(arr.map(item => Process(item)));


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Boom! Execution time dropped from 5 seconds to 0.5 seconds. As a back-end dev, I now use this trick to turbocharge my APIs.

Lesson learned: If you don’t need strict order, avoid await inside loops. Always check if the task can be parallelized.

Here’s the full code to try it yourself:


async function Process(data) {
return new Promise(resolve => {
setTimeout(() => {
console.log(data);
resolve();
}, 500);
});
}

async function main() {
const start = process.hrtime.bigint();

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


for (const item of arr) {
await Process(item);
}

// parallelized
// await Promise.all(arr.map(item => Process(item)));

const end = process.hrtime.bigint();
const durationNanoseconds = end - start;
const durationSeconds = Number(durationNanoseconds) / 1_000_000_000;

console.log(`Sequential execution time: ${durationSeconds.toFixed(3)} seconds`);
}

main();

See all posts:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


Thanks for reading!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу