Benchmark: subtring vs substr


After running benchmark for js substring vs substr I found the following results:

Substring:

(() => {
 let start = +Date.now();
 for (let i = 0; i < 1000000000; i++) {
	let val = "0.fr8gdqxstse".substring(2)
  }
 console.log(+Date.now() - start);
})();

This gave me run time of 21447ms

With end index:

(() => {
 let start = +Date.now();
 for (let i = 0; i < 1000000000; i++) {
	let val = "0.fr8gdqxstse".substring(2, 10)
  }
 console.log(+Date.now() - start);
})();

This ran for 21271ms

Substr:

(() => {
 let start = +Date.now();
 for (let i = 0; i < 1000000000; i++) {
	let val = "0.fr8gdqxstse".substr(2)
  }
 console.log(+Date.now() - start);
})();

This gave me run time of 25522ms

With size:

(() => {
 let start = +Date.now();
 for (let i = 0; i < 1000000000; i++) {
	let val = "0.fr8gdqxstse".substr(2, 8)
  }
 console.log(+Date.now() - start);
})();

Run time: 22510ms

After running several iterations, this was average result.

Conclusion:
substring runs faster.

I may have missed something. Please comment if you like.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s