# Removing the First Item: Linked List vs. Array

**URL:** <https://forum.kirupa.com/t/removing-the-first-item-linked-list-vs-array/658556>\
**Category:** random\
**Created:** [May 5, 2023, 4:56am UTC](https://forum.kirupa.com/t/removing-the-first-item-linked-list-vs-array/658556 "2023-05-05T04:56:19Z")\
**Posts on this page:** 3\
**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:** [May 5, 2023, 4:56am UTC](https://forum.kirupa.com/t/removing-the-first-item-linked-list-vs-array/658556/1 "2023-05-05T04:56:19Z")

</div>

For fun, I decided to calculate how much faster a linked list is at removing the first item compared to an array (using `array.shift()`). As it turns out, on average, when dealing with 10,000 items, a linked list is about 400% faster compared to an array.

Here is the example:

```auto
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://www.kirupa.com/js/linkedlist_v1.js"></script>
</head>

<body>
  <script>
    let myList = new LinkedList();
    let start, end;

    for (let i = 0; i < 10000; i++) {
      let num = Math.round(Math.random() * 10000);
      myList.addFirst(num);
    }

    start = performance.now();

    for (let i = 0; i < 10000; i++) {
      myList.removeFirst();
    }

    end = performance.now();
    console.log("Elapsed time for linkedlist is: " + (end - start));

    let myArray = [];

    

    for (let i = 0; i < 10000; i++) {
      let num = Math.round(Math.random() * 10000);
      myArray.push(num)
    }

    start = performance.now();

    for (let i = 0; i < 10000; i++) {
      myArray.shift();
    }

    end = performance.now();
    console.log("Elapsed time for arrays is: " + (end - start));
  </script>
</body>

</html>

```

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [May 5, 2023, 7:08am UTC](https://forum.kirupa.com/t/removing-the-first-item-linked-list-vs-array/658556/2 "2023-05-05T07:08:57Z")

</div>

Yeah, but ya cheating 😜  
At not time do you actually remove an item, you only change a reference to what the first item is. The item that was originally pointed to still exists. Wonder how the GC would eventually react to this.

---

<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:** [May 5, 2023, 1:48pm UTC](https://forum.kirupa.com/t/removing-the-first-item-linked-list-vs-array/658556/3 "2023-05-05T13:48:03Z")

</div>

That would be an implementation detail 😅

If you remove the last item, the performance between both are very similar!
