JavaScript engines, such as V8 (used in Chrome and Node.js) and SpiderMonkey (used in Firefox), continuously optimize JavaScript code for performance. One key area of optimization is how arrays are handled internally. In this blog, we will explore JavaScript array optimizations and how the JavaScript engine categorizes arrays for efficient execution.
Additionally, we will look at how to install JavaScript engines using JSVU (JavaScript Engine Version Updater), a tool that allows developers to easily install and manage multiple JavaScript engines.
these 3 types are possible in both continuous and holey
so, based on what elements are present and what are there position in array, internally javascript optimizes it.
Given array has 0,1,2 element position, also checks what are the values, are strings, numbers, etc. That is how optimization happens in V8 and other engines.
Technical Communication:
As much the technical communication is stronger becomes the more you can have pair programming with others.
In array, the every subtances are are called as elements of array. As we say for object has properties, we say array contain elements not properties
So, which method will you use for based on there implementation:
for-each on array:
optimization will differ based on types, is it string, float, integer, continous or holey, optimization differs here
Optimization does mean only to study linked list or dynamic programming, it is also mean to optimize things based on usecase and scenarios, that is the engineering side.
const myArr = []// %DebugPrint(myArr)// const arrTwo = [1, 2, 3, 4, 5]// This is packed ( or continuous, not technically called as continuous) SMI elements, because every position holds value// const arrThree = [1,2, , 4, 5]// This is holey elements, because one position holds no valueconst arrTwo = [1, 2, 3, 4, 5]// This is the best type of array, but are very restricted, you can only take numbers only.arrTwo.push(6.0)// It is now packed double elements// It contains a double elementarrTwo.push('7')// It is now packed elements// It will have no different optimization.// When the packed SMI elements converted to other, there is no way to convert it back to packed SMI elements.arrTwo[10] = 11// Now, it becomes holey elements and have gaps, and the optimization will be in a different way.console.log(arrTwo)console.log(arrTwo.length);console.log(arrTwo[9]);// These undefined operations are very costly operations// Whenever you want to access some elements from an array.// First happens 'bound check'// it checks from the starting element, array length, if the it is outside the array length, for example 19th position, it will quickly show undefined.// But when you ask for 9th position which is inside the array length but no values is there, it is undefined, and this is very different case from the previous one.// Since, array out of bound check is passed successfully.// Step 2, checking the property or any value here, hasOwnProperty(arrTwo, 9), if false and found no value then in prototype// Check property again, hasOwnProperty(arrTwo.prototype, 10)// if not found, since we know, JavaScript has prototypal nature, it keeps on checking until get null value.// So, now checking at hasOwnProperty(Object.prototype, 10)// Hence, the hasOwnProperty check is one of the most expensive check inside javascript, that is why it said holes are expensive.// That is why it is recommended to avoid holes in arrayconst arrThree = [1, 2, 3, 4, 5]console.log(arrThree[8]);// Since, it is out of bound, bound check is done, it will return undefined, optimization is done here.console.log(arrThree[2]);// Since it is continuous, no holes, no out of bound, then return the value, done task.// Optimzed based array// SMI > Double > Packed Elements ---- Continous Array// Hole_SMI > Hole_Double > Hole_Packed ---- Holey Array// Note:// If SMI is changed to double, it can't be reverted even you remove the double elementconst arrFour = new Array(3)// It has 3 holes, it is now holey SMI Elements// When downgrade starts:arrFour[0] = '1' // Now it is holey elements, there is no other way to revert it.// What we did, first kept the array empty.// Optimization is less prioritized than UsecasearrFour[0] = '1'arrFour[1] = '1'arrFour[2] = '1'// What better we can doconst arrFive = []// We just keeping 3 positions empty, we took an array which is empty, it is little optimized.// Now, we can push elements.arrFive.push('1') // It is not holey elements, it is now packed elements.arrFive.push('2')arrFive.push('3')const arrSix = [1,2,3,4,5] // SMI Elements// If you added elements such as NaNarrSix.push(NaN) // it is now packed Double elementsarrSix.push(Infinity)// Even you remove the element, it is now downgraded to packed double and it can't be SMI packed now// In modern optimization by V-8 or Spider-Monkey engine, the recommendation is given: either for for loop or for-of loop, or forEach or any other loop, it is advised to use methods prefered by the JavaScript because you thought you have optimised for 2 or 3 things, but the browser has optimised it for many things, so use the by default methods