# Kirupa.com - Hashtables vs. Arrays

**URL:** <https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755>\
**Category:** programming\
**Created:** [November 23, 2014, 11:23am UTC](https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755 "2014-11-23T11:23:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 23, 2014, 11:23am UTC](https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755/1 "2014-11-23T11:23:13Z")

</div>

by [kirupa](http://www.kirupa.com/me/index.htm) | 10 December 2011

[Have questions? Discuss this HTML5 / JavaScript tutorial with others on the forums.](http://www.kirupa.com/forum/showthread.php?370247-Tutorial-Hashtables-vs.-Arrays)

* * *
This is a companion discussion topic for the original entry at [http://www.kirupa.com/html5/hashtables\_vs\_arrays.htm](http://www.kirupa.com/html5/hashtables_vs_arrays.htm)

---

<div class="post-metadata">

**Author:** ![Shupeng\_Xu](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Shupeng\_Xu](https://forum.kirupa.com/u/Shupeng_Xu)\
**Post date:** [April 28, 2015, 5:46am UTC](https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755/2 "2015-04-28T05:46:02Z")

</div>

Hi, I think there may be some question here:  
Inserting (array) O(1) - Constant

Can you explain this please ?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [April 28, 2015, 7:00pm UTC](https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755/3 "2015-04-28T19:00:46Z")

</div>

@kirupa doesn’t describe what he means by ‘inserting’ for the array case, so I wouldn’t worry about it too much. If you look at the table here:

> **[Dynamic array | Performance](https://en.wikipedia.org/wiki/Dynamic_array#Performance)**
>
> The dynamic array has performance similar to an array, with the addition of new operations to add and remove elements: 1.Getting or setting the value at a particular index (constant time) |
> 2.Iterating over the elements in order (linear time, good cache performance) |
> 3.Inserting or deleting an element in the middle of the array (linear time) |
> 4.Inserting or deleting an element at the end of the array (constant amortized time) Dynamic arrays benefit from many of the advantages of arrays, incl...

…you can see that he probably means appending to the end of a dynamic array takes O(1) on average. A short explanation is that the length of the array is known, so it’s easy to calculate address of where the newly appended element goes. Memory-wise, either there’s going to be space available immediately after the end of the array, in which case the operation takes constant time, or there’s not space, in which case some sort of reallocation occurs and could take a non-constant amount of time.

---

<div class="post-metadata">

**Author:** ![Faiz\_Hameed](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/faiz_hameed/32/10002_2.png) [@Faiz\_Hameed](https://forum.kirupa.com/u/Faiz_Hameed)\
**Post date:** [September 12, 2019, 6:58am UTC](https://forum.kirupa.com/t/kirupa-com-hashtables-vs-arrays/352755/4 "2019-09-12T06:58:06Z")

</div>

Basically , if you are searching for a particular item in a long data, hashtable becomes a clear winner, which is a major part of any application that handles data
