Note that there are some explanatory texts on larger screens.

plurals
  1. POBig O of JavaScript arrays
    primarykey
    data
    text
    <p>Arrays in JavaScript are very easy to modify by adding and removing items. It somewhat masks the fact that most languages array's are fixed-size, and require complex operations to resize. It seems that JavaScript makes it easy to write poorly performing array code. This leads to the question:</p> <p><strong>What performance (in terms of big O time complexity) can I expect from JavaScript implementations in regards to array performance?</strong></p> <p>I assume that all reasonable JavaScript implementations have at least the following big O's.</p> <ul> <li>Access - O(1)</li> <li>Appending - O(n)</li> <li>Prepending - O(n)</li> <li>Insertion - O(n)</li> <li>Deletion - O(n)</li> <li>Swapping - O(1)</li> </ul> <p>JavaScript lets you pre-fill an array to a certain size, using <code>new Array(length)</code> syntax. (Bonus question: Is creating an array in this manner O(1) or O(n)) This is more like a conventional array, and if used as a pre-sized array, can allow O(1) appending. If circular buffer logic is added, you can achieve O(1) prepending. If a dynamically expanding array is used, O(log n) will be the average case for both of those.</p> <p>Can I expect better performance for some things than my assumptions here? I don't expect anything is outlined in any specifications, but in practice it could be that all major implementations use optimized arrays behind the scenes. Are there dynamically expanding arrays or some other performance boosting algorithms at work?</p> <p>P.S.</p> <p>The reason I'm wondering this is because I'm researching some sorting algorithms, most of which seem to assume appending and deleting are O(1) operations when describing their overall big O.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload