From b1577cd97272354f930ee506a09a4d930903c68c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 15 Jan 2024 20:53:52 +0100 Subject: [PATCH] perf: use object literal in Deque doubly linked list MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/deque.ts | 24 +++++++++--------------- src/index.ts | 2 +- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/deque.ts b/src/deque.ts index 44431789..027aa70d 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -1,19 +1,15 @@ // Copyright Jerome Benoit. 2023. All Rights Reserved. /** - * Linked list node. + * Linked list node interface. * * @typeParam T - Type of linked list node data. * @internal */ -export class LinkedListNode { - public data: T - public next?: LinkedListNode - public prev?: LinkedListNode - - public constructor (data: T) { - this.data = data - } +export interface ILinkedListNode { + data: T + next?: ILinkedListNode + prev?: ILinkedListNode } /** @@ -24,8 +20,8 @@ export class LinkedListNode { * @internal */ export class Deque { - private head?: LinkedListNode - private tail?: LinkedListNode + private head?: ILinkedListNode + private tail?: ILinkedListNode /** The size of the deque. */ public size!: number /** The maximum size of the deque. */ @@ -42,11 +38,10 @@ export class Deque { * @returns The new size of the queue. */ public push (data: T): number { - const node = new LinkedListNode(data) + const node = { data, prev: this.tail } if (this.tail == null) { this.head = this.tail = node } else { - node.prev = this.tail this.tail = this.tail.next = node } return this.incrementSize() @@ -59,11 +54,10 @@ export class Deque { * @returns The new size of the queue. */ public unshift (data: T): number { - const node = new LinkedListNode(data) + const node = { data, next: this.head } if (this.head == null) { this.head = this.tail = node } else { - node.next = this.head this.head = this.head.prev = node } return this.incrementSize() diff --git a/src/index.ts b/src/index.ts index 1548487f..eab1ab2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,5 +74,5 @@ export type { Writable } from './utility-types.js' export type { CircularArray } from './circular-array.js' -export type { Deque, LinkedListNode } from './deque.js' +export type { Deque, ILinkedListNode } from './deque.js' export { availableParallelism } from './utils.js' -- 2.34.1