Skip to content

Commit

Permalink
lib: inline Array operations in FreeList methods
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
ExE-Boss and aduh95 committed Mar 7, 2021
1 parent d63b717 commit 326ddd9
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/internal/freelist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;

Expand All @@ -9,18 +10,29 @@ class FreeList {
this.name = name;
this.ctor = ctor;
this.max = max;
this.list = [];
this.list = ObjectSetPrototypeOf([], null);
}

alloc() {
return this.list.length > 0 ?
this.list.pop() :
ReflectApply(this.ctor, this, arguments);
const { list } = this;
const { length } = list;

if (length > 0) {
const lastIndex = length - 1;
const result = list[lastIndex];
list.length = lastIndex;
return result;
}

return ReflectApply(this.ctor, this, arguments);
}

free(obj) {
if (this.list.length < this.max) {
this.list.push(obj);
const { list } = this;
const { length } = list;

if (length < this.max) {
list[length] = obj;
return true;
}
return false;
Expand Down

0 comments on commit 326ddd9

Please sign in to comment.