Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark: measure the run time of the script on the html x times, then do the average. #1889

Open
Palloxin opened this issue Sep 22, 2024 · 4 comments

Comments

@Palloxin
Copy link

Palloxin commented Sep 22, 2024

Description

I would like a way to measure the run code x times on the html and then get the average time, for easy test purposes.

Would this feature be feasible?

To cope I currently use performance.now() to measure the run time of the code like here: https://jsfiddle.net/udq02rjL/
(I made the html very long with lots of strings)
I would like the website to do that for easy comfort testing.

Other Details

Regex101 just added the benchmark feature where it applies the regex continuously for 10 seconds.
You can try it here: https://regex101.com/r/eIqVlC/1
image

@oskarkrawczyk
Copy link
Member

Over the years I had only one request of such a feature (yours), so I don't feel like our users are the target-group for such a feature.

I'll leave this in as a Feature Suggestion for now.

@oskarkrawczyk oskarkrawczyk changed the title [Feature request] Benchmark: measure the run time of the script on the html x times, then do the average. Benchmark: measure the run time of the script on the html x times, then do the average. Sep 24, 2024
@Palloxin
Copy link
Author

Palloxin commented Sep 24, 2024

Maybe because they are not newbies. I just started and like to experiment what code is faster etc..
Maybe someone experienced could do a loop and do the exact same I asked.

This feature would be a very comfortable assistance from jsfiddle part, with a nice little button

@Palloxin
Copy link
Author

Palloxin commented Sep 25, 2024

// Function to run your code
function yourCode() {
    // Replace this with whatever code you want to run repeatedly
    // For example, a simple math operation like addition
    let sum = 0;
    for(let i = 0; i < 1000; i++) {
        sum += i;
    }
    return sum;
}

function runTest() {
    const duration = 60000; // Run for 60 seconds
    const startTime = performance.now(); // Get start time
    let currentTime = startTime;
    let totalRuns = 0;
    let totalRunTime = 0;
    let minRunTime = Infinity; // Initialize minimum runtime

    // Loop until the elapsed time exceeds 10 seconds
    while (currentTime - startTime < duration) {
        const runStartTime = performance.now(); // Start time for this run
        yourCode(); // Call the function repeatedly
        const runEndTime = performance.now(); // End time for this run

        const runTime = runEndTime - runStartTime; // Calculate the run time
        totalRunTime += runTime; // Accumulate total run time
        totalRuns++; // Increment the number of runs

        // Update minimum run time
        if (runTime < minRunTime) {
            minRunTime = runTime;
        }

        currentTime = performance.now(); // Update the current time
    }

    // Calculate the average time per run
    const averageTime = totalRunTime / totalRuns;

    console.log(`Total runs: ${totalRuns}`);
    console.log(`Total time spent: ${totalRunTime.toFixed(2)} ms`);
    console.log(`Average time per run: ${averageTime.toFixed(2)} ms`);
    console.log(`Minimum run time: ${minRunTime.toFixed(2)} ms`);
}

// Start the test
runTest();

This is an example of how the feature can be reproduced on the user part. It's just annoying to set up.
This issue is basically about a QoL feature.
I didnt see a similar thing on JSFiddle alike websites. It would be a good addition to make it more unique.

JUST a nice and comfortable button to trigger the test, so good.
The code above bugs if the loop time is too high, on my machine 60 seconds it's too high

@Palloxin
Copy link
Author

Palloxin commented Sep 25, 2024

PS:
-People never asked for electricity before its discovery
-People can't crave for chocolate if they never tasted or heard of it.

Give people a taste of the benchmark feature, they will love it (even because nobody else has it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants