Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me to get which is the fastest way to duplicate an array using in JavaScript?

1 Answer

0 votes
by (26.7k points)

Basically, the fastest one will depends on the browser like for Blink browser slice() is fastest, whereas for other browsers while loop is fastest. Also, you can use the below code to test in the browser console:

While Loop:

n = 1000*1000;

start = + new Date();

a = Array(n); 

b = Array(n); 

i = a.length;

while(i--) b[i] = a[i];

console.log(new Date() - start);

Slice :

n = 1000*1000;

start = + new Date();

a = Array(n); 

b = a.slice();

console.log(new Date() - start);

I hope this will help.

Want to become a Java Expert? Join Java Certification now!!

Want to know more about Java? Watch this video on Java Tutorial for Beginners | Java Programming:

Related questions

0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 6, 2021 in Java by dante07 (13.1k points)
0 votes
0 answers
asked Feb 18, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
asked Jan 27, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

Browse Categories

...