@anonymous/

AromaticAgitatedProfessionals

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function copyReverse(array) {
  const arrayLength = array.length;
  const copy = Array(arrayLength);

  for (let index = 0; index < arrayLength; index++) {
    copy[index] = array[arrayLength - 1 - index];
  }

  return copy;
}

const original = [..."Hello"];
const reversed = copyReverse(original);

console.log(`Original: ${JSON.stringify(original)}`); // Original: ["H","e","l","l","o"]
console.log(`Reversed: ${JSON.stringify(reversed)}`); // Reversed: ["o","l","l","e","H"]

console.time("with slice and reverse");

for (let index = 0; index < 100000; index++) {
  original.slice().reverse();
}

console.timeEnd("with slice and reverse");

console.time("With one loop");

for (let index = 0; index < 100000; index++) {
  copyReverse(original);
}

console.timeEnd("With one loop");
node v10.16.0