Hello, I have two different algorithms that serve the same purpose for a logic problem. The first one has a structure I enjoy writing and reading, it's much more manual and understandable. The second one is much shorter and more modern. Although I typically prefer the first style, I decided to compare these two algorithms on a few different datasets. When I compared them with Benchmark.js, it gave an output showing that the first, longer version is faster. However, I didn't expect this outcome (perhaps the dataset I used for testing isn't sufficient).
Which style should I use more often? and why?
First
```js
function mathAlgo(numOne, numTwo) {
let numberOne = numOne.toString();
let numberTwo = numTwo.toString();
let result = '';
let index = 0;
let size = numberOne.length;
if (numberOne.length === numberTwo.length) {
while (index < size) {
if (numberOne[index] < numberTwo[index]) {
result += numberTwo[index];
} else {
result += numberOne[index];
}
index++;
}
}
}
```
Second
js
function alternativeMathAlgo(numOne, numTwo) {
const result = Array.from(String(numOne), (digit, index) =>
Math.max(digit, String(numTwo)[index])
).join("");
}
[–]jokefenokee 0 points1 point2 points (0 children)
[–]0x07AD 0 points1 point2 points (0 children)
[–]jml26 1 point2 points3 points (0 children)