Skip to content

Commit

Permalink
perf(LinkedList): remove static function as it affects v8 inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Jun 10, 2017
1 parent 76c8cff commit c65e337
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
10 changes: 1 addition & 9 deletions src/lib/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export class LinkedListNode<T> {
this.right = undefined
this.left = undefined
}

static of <T> (val: T) {
return new LinkedListNode(val)
}
}

// TODO: Use singly linked list
Expand All @@ -33,7 +29,7 @@ export class LinkedList<T> {


add (val: T) {
const node = LinkedListNode.of(val)
const node = new LinkedListNode(val)
if (this.length === 0) this.__head = node
if (!this.__tail) {
this.__tail = node
Expand Down Expand Up @@ -72,8 +68,4 @@ export class LinkedList<T> {
}
this.length > 0 ? this.length-- : void 0
}

static of<T> () {
return new LinkedList<T>()
}
}
18 changes: 9 additions & 9 deletions test/test.LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ function toArray<T> (q: LinkedList<T>) {
return arr
}
test('constructor()', t => {
t.true(LinkedList.of() instanceof LinkedList)
t.true(new LinkedList() instanceof LinkedList)
})
test('add()', t => {
const q = LinkedList.of()
const q = new LinkedList()
q.add('A')
q.add('B')
q.add('C')
Expand All @@ -25,7 +25,7 @@ test('add()', t => {
})

test('remove(): Remove First (non-empty)', t => {
const q = LinkedList.of()
const q = new LinkedList()
const a = q.add('A')
q.add('B')
q.add('C')
Expand All @@ -38,7 +38,7 @@ test('remove(): Remove First (non-empty)', t => {
})

test('remove(): Remove LAST (non-empty)', t => {
const q = LinkedList.of()
const q = new LinkedList()
q.add('A')
q.add('B')
const c = q.add('C')
Expand All @@ -52,7 +52,7 @@ test('remove(): Remove LAST (non-empty)', t => {


test('remove(): Remove MIDDLE (non-empty)', t => {
const q = LinkedList.of()
const q = new LinkedList()
q.add('A')
const b = q.add('B')
q.add('C')
Expand All @@ -66,7 +66,7 @@ test('remove(): Remove MIDDLE (non-empty)', t => {


test('remove(): Remove LAST', t => {
const q = LinkedList.of()
const q = new LinkedList()
const a = q.add('A')
q.remove(a)

Expand All @@ -78,7 +78,7 @@ test('remove(): Remove LAST', t => {

test('forEach()', t => {
const results: number[] = []
const q = LinkedList.of<number>()
const q = new LinkedList<number>()
q.add(1)
q.add(2)
q.add(3)
Expand All @@ -89,9 +89,9 @@ test('forEach()', t => {


test('remove(): Remove LAST more than once', t => {
const q = LinkedList.of()
const q = new LinkedList()
const a = q.add('A')
q.remove(a)
q.remove(a)
t.is(q.length, 0)
})
})

0 comments on commit c65e337

Please sign in to comment.