So, I'm the people of hashnode can help me out here. I urge everyone to read the full question before answering!!
When I say optimise I mean the execution times. I.e. which loops are faster, how to minimise memory size and so on.
I DO NOT mean network speeds like downloads, caching etc.
I wondered if anyone knows any books or articles about this topic. I've found some myself but resources seem to be getting a bit thin now.
I primarily need resources targeting strings, objects & arrays, DOM interaction and for/if methods,and as a final thought anything on webpack. However, any resources would be greatly appreciated.
In general build in functions (like Array.prototype.forEach ) are always optimised.
I really like Quokkas speed test. But unfortunately it is a pro feature and only compares the speed in V8. Operations are differently optimized in different runtimes. So you have to test your functions in different environments.
You can use the developer tools of the different browsers to compare the memory usage in that browsers.
So, first let me sort out your question. Am I correct?
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.
constby 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;