Last active
August 2, 2016 20:16
-
-
Save alexhsamuel/29ed0e9f084f530ae37895b0d81aa5f0 to your computer and use it in GitHub Desktop.
Javascript linked list complexity demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var now; | |
if (typeof performance !== 'undefined') { | |
// In the browser. | |
now = function () { | |
return performance.now() * 1E-3; | |
} | |
} else { | |
// In node. | |
now = function () { | |
var hr = process.hrtime(); | |
return hr[0] + hr[1] * 1E-9; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
// Simple singly linked list | |
//------------------------------------------------------------------------------ | |
var NO_NODE = null; | |
function Node(value, next) { | |
this.value = value; | |
this.next = next; | |
} | |
function List() { | |
this.head = NO_NODE; | |
} | |
List.prototype.toString = function () { | |
var string = '['; | |
for (var node = this.head; node; node = node.next) { | |
string += node.value; | |
if (node.next) string += ', '; | |
} | |
string += ']'; | |
return string; | |
} | |
List.prototype.unshift = function (value) { | |
// Replace the head with our new node, with the old head following it. | |
this.head = new Node(value, this.head); | |
} | |
List.prototype.push = function (value) { | |
// Create the new node for this value. Since it will be the last element | |
// on the list, it has no next node. | |
var newNode = new Node(value, NO_NODE); | |
if (this.head === NO_NODE) { | |
// This is the first element in the list, so the new node is the head node. | |
this.head = newNode; | |
} else { | |
// Find the last node in the list, by starting at the head node and walking | |
// down the list until we find one that has no next node. | |
var tail = this.head; | |
while (tail.next !== NO_NODE) tail = tail.next; | |
// The new node comes after the old tail node. | |
tail.next = newNode; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
/** | |
* Constructs a linked list of the numbers 0 through n - 1. | |
* | |
* Uses `List.prototype.push`. | |
*/ | |
function countPush(n) { | |
var numbers = new List(); | |
for (var i = 0; i < n; ++i) numbers.push(i); | |
return numbers; | |
} | |
console.log("countPush:") | |
for (var n = 1000; n <= 25000; n += 1000) { | |
var start = now(); | |
var numbers = countPush(n); | |
var end = now(); | |
console.log(n, end - start); | |
} | |
//------------------------------------------------------------------------------ | |
/** | |
* Constructs a linked list of the numbers 0 through n - 1. | |
* | |
* Uses `List.prototype.unshift`. | |
*/ | |
function countUnshift(n) { | |
var numbers = new List(); | |
for (var i = n - 1; i >= 0; --i) numbers.unshift(i); | |
return numbers; | |
} | |
console.log("\ncountUnshift:") | |
for (var n = 1000; n <= 25000; n += 1000) { | |
var start = now(); | |
var numbers = countUnshift(n); | |
var end = now(); | |
console.log(n, end - start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment