So, first let me sort out your question. Am I correct?
How to optimise JS [..] execution times. I.e. [..] loops [..], strings, objects & arrays, DOM interaction and for/if methods
While you get the most performance out of algorithmic improvements, you can try to write better performance-optimized code even from the start, as long as you are careful to keep the code readable and maintainable. Also, if there is a bottleneck in one place and you don't know how to optimize the algorithm, knowing a few tricks can be beneficial. Though you might want to consider ditching JS for such performance-relevant code and use WASM or native Node modules instead!
Well then, here is what I know :) Some of the stuff below might lead to quite an aggressive optimization at the cost of readability and maintainability, so be sure that you really want to follow my tips.
const by putting the mutability into an arrow function)Examples (ran bench 10 times and took average)
/// Built-In Functions (jsben.ch/fajXS - ~63% faster for me):
const arr = [1,2,3];
// Instead of
let sum = 0;
for (const num of arr) { sum += arr; }
// write
sum = arr.reduce((acc, num) => acc + num);
/// Branches (jsben.ch/h0SWo - ~14% faster for me):
const a = -5;
const b = 3;
let c = 1;
// Instead of
if (a < 0) { c += b * a; }
// write
c += b * Math.min(a, 0);
/// Functions (jsben.ch/thqBL - ~16-20% faster for me):
const obj = { a: 1, b: 2, c: 3, };
// Instead of
const a = (() => {
let tmp = 0;
for (const key in obj) {
tmp += parseInt(obj[key]) || 0;
}
return tmp;
})();
// write
let a_tmp = 0;
for (const key in obj) {
a_tmp += parseInt(obj[key]) || 0;
}
const a = a_tmp; // only if you really want a const...
/// Recursion (jsben.ch/Q1ODq - ~7% faster for me):
const arr = const arr = ((length, max) => [...new Array(length)]
.map(() => Math.round(Math.random() * max)))(10000, 100);
// Instead of
function bubbleSortRecursive(array, index1, index2) {
index1 = typeof index1 == 'undefined' ? 0 : index1;
index2 = typeof index2 == 'undefined' ? array.length : index2;
if (index1 > index2) {
return array;
} else if (index1 == index2 - 1) {
return bubbleSortRecursive(array, 0, index2 - 1);
} else if (array[index1] > array[index1 + 1]) {
const temp = array[index1];
array[index1] = array[index1 + 1];
array[index1 + 1] = temp;
return bubbleSortRecursive(array, index1 + 1, index2);
} else {
return bubbleSortRecursive(array, index1 + 1, index2);
}
}
bubbleSortRecursive(arr);
// write
function bubbleSort(arr) {
let sorted = false;
const sort = (element, index, array) => {
if (element > array[index + 1]) {
array[index] = array[index + 1];
array[index + 1] = element;
sorted = false;
}
};
while (!sorted) {
sorted = true;
arr.forEach(sort);
}
}
bubbleSort(arr);
/// Loops 1 (jsben.ch/re3XJ - ~33% faster for me):
const arr = [1,2,3];
// Instead of
const sum = arr.reduce((acc, num) => acc + num);
// write
const sum = arr[0] + arr[1] + arr[2];
/// Loops 2:
const arr = ((length, max) => [...new Array(length)]
.map(() => Math.round(Math.random() * max)))(10000, 100);
// Instead of
for (let i = 0; i < getNumSomething(); i++) { doSth(arr[i]); }
// write
const length = getNumSomething();
for (let i = 0; i < length; i++) { doSth(arr[i]); }
/// Strings (jsben.ch/yfNlX - ~8% faster for me)
const costCat = {
banana: 3.5,
apple: 1.25,
salad: 2.76,
};
// Instead of
const price = costCat['banana'];
// write
const price = costCat.banana;