Socket
Book a DemoInstallSign in
Socket

benchmark.js

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

benchmark.js

Simple benchmarking script for Javascript functions to compare execution times.

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
3
-25%
Maintainers
1
Weekly downloads
 
Created
Source

benchmark.js

Simple benchmarking script for Javascript functions to compare execution times.

Usage

The more iterations you choose the more accurate the benchmarking results will be, note that choosing a high amount of iterations will take a long time for the benchmark to complete.

iterations parameter takes an interger number for how many loops you want the benchmark to run, function1 and function2 parameters should be javascript function(){ // code } or () => { // code } function code blocks.

<script src="bjs.min.js"></script>
<script>
bjs(iterations, function1, function2);
</script>

Example

I want to compare getting the current minutes with leading zero's, using the short-if statement or slice function so I use benchmark.js as follows:

<script src="bjs.min.js"></script>
<script>
// Variables needed for functions to work
var dateObj = new Date();
var minutes = dateObj.getMinutes();
var voidVar;
// Example 1
bjs(1000000, function(){
	voidVar = ('0'+minutes).slice(-2);
},
function(){
	voidVar = (minutes < 10 ? '0' : '') + minutes;
});
// Example 2, one-liner:
bjs(100000, () => {voidVar = ('0'+minutes).slice(-2)}, () => {voidVar = (minutes < 10 ? '0' : '') + minutes});
</script>

Output

Output is send to the Browser's console as an object (console.log is used):

{
  "Function 1": "2.611s (2611ms)"
  "Function 2": "1.735s (1735ms)"
  "iterations": 10000000
}

Keywords

javascript

FAQs

Package last updated on 18 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts