Giter Site home page Giter Site logo

jsspeedtest's Introduction

Simple JavaScript Speed Test Class

While console.time() can be used to measure the speed of an operation, it has two inherent problems.

  1. It lacks the ability to perform a truly accurate test, in that the results will vary each time the code is run.

  2. The execution of console.time() itself can greatly add to the overall execution time.

This class provides a more efficient alternative, as well as providing a more accurate result by returning an average execution time based on a given number of repetitions.

Usage

Say you wanted to measure the execution time of the code to add new members to a list of current members:

var membersNew = ["Cristen Banton", "Kirsten Thiede", "Cyril Punch", "Layne Perillo", "Faviola Leaver"],
    membersCurrent = ["Regan Padillo", "Jay Shuford", "Ferne Amey", "Garrett Hem", "Delila Embrey"];

for (var i = 0; i < membersNew.length; i++) {
  membersCurrent.push(new Member(membersNew[i], 1));
}

You would create a new list referencing the member lists:

var listsForTests = [membersNew, membersCurrent];

Then create a function expression that contains the code to be tested, changing any references to the original lists to reference the new listOfParams argument passed in to the anonymous function:

var addMembers = function(listOfParams) {
  for (var i = 0; i < listOfParams[0].length; i++) {
    listOfParams[1].push(new Member(listOfParams[0][i], 1));
  }
};

Now instantiate the SpeedTest class, passing in as parameters the function expression, the list of member lists, and optionally the number of repetitions to execute the code. If you don't specify the number of repetitions, it defaults to 10,000.

var speedTest = new SpeedTest(addMembers, listsForTests);

-or-

var speedTest = new SpeedTest(addMembers, listsForTests, 100000);

Finally call the startTest method of the SpeedTest class instance:

speedTest.startTest();

Your finished code would look like this:

var membersNew = ["Cristen Banton", "Kirsten Thiede", "Cyril Punch", "Layne Perillo", "Faviola Leaver"],
    membersCurrent = ["Regan Padillo", "Jay Shuford", "Ferne Amey", "Garrett Hem", "Delila Embrey"],
    listsForTests = [membersNew, membersCurrent];

var addMembers = function(listOfParams) {
  for (var i = 0; i < listOfParams[0].length; i++) {
    listOfParams[1].push(new Member(listOfParams[0][i], 1));
  }
};

var speedTest = new SpeedTest(addMembers, listsForTests, 100000);
speedTest.startTest();

The result would be logged to the console as such:

> Average execution across 100000: .0043ms

License

BSD license

jsspeedtest's People

Watchers

Jamey M. Yates avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.