question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Incorrent timig with for loops

See original GitHub issue

This is my code which I am testing

import { Bench } from "tinybench";

const bench = new Bench({ iterations: 100_000 });

bench
  .add("loop 1", () => {
    let sum = 1;
    for (let index = 0; indexindex < 10000; indexindex++) {
      sum = sum * index;
    }
  })
  .add("loop 2", () => {
    let sum = 1;
    for (let index = 0; indexindex < 10; indexindex++) {
      sum = sum * index;
    }
  });

await bench.run();

console.table(
  bench.tasks.map(({ name, result }) => ({
    "Task Name": name,
    "Time (ps)": result?.totalTime,
    "Variance (ps)": result?.variance * 1000,
  }))
);

The results are same in both

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ (index) โ”‚ Task Name โ”‚      Time (ps)       โ”‚    Variance (ps)     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚    0    โ”‚ 'loop 1'  โ”‚ 0.002133705863210011 โ”‚ 0.010551717975465412 โ”‚
โ”‚    1    โ”‚ 'loop 2'  โ”‚ 0.002180478539965521 โ”‚ 0.030143625437354347 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

I am using node v16.17.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kainstarcommented, Oct 17, 2022

Itโ€™s your codeโ€™s error, not tinybenchโ€™s problem.

Please check your for loop end condition

for (let index = 0; indexindex < 10000; indexindex++)

You declare a veriable named index, but the end condition is using a undeclared varibale indexindex.

These two code has same time because when it execute, it would throw error immediately and never into loop.

When I test correct code on v2.3.0, it give such result: image

0reactions
Aslemammadcommented, Oct 17, 2022

Please create a new issue in case! I donโ€™t think this is a problem anymore. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Slow for loop with inconsistent timing - c++ - Stack Overflow
When I run this code, I get dramatic inconsistencies in the between the timing in the inner loop and outer loop. E.g. the...
Read more >
Risks & Errors in While, For & Do While Loops in C - Study.com
The potential risks and errors can be divided into two broad categories: problems with the loop control (running the loop wrong number of...
Read more >
Pro Toolsยฎ SE - Fixing the timing of an Audio Loop - YouTube
This video explains how easy it is to fix the timing of a piece of recorded audio, or an existing audio loop. It...
Read more >
Loops: while(), for() and do .. while() - Physics and Astronomy
In this rather contrived example, x is halved every time the loop is executed until at last the test condition is false and...
Read more >
Using a for-loop or sleeping to wait for short intervals of time
The biggest problem with using a for-loop to do this is that you are wasting CPU power. When using sleep , the CPU...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found