Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ const {
Array: ArrayConstructor,
ArrayPrototypeForEach,
ArrayPrototypeMap,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
FinalizationRegistry,
FunctionPrototypeCall,
Map,
Expand Down Expand Up @@ -720,5 +722,26 @@ primordials.SafeStringPrototypeSearch = (str, regexp) => {
return match ? match.index : -1;
};

/**
* Variadic functions with lots of arguments will cause stack overflow errors.
* Use this function when `items` can be arbitrarily large, this function splits
* it into chunks of size 2**16 making stack overflow less likely.
* @param {Array<unknown>} arr
* @param {Parameters<typeof Array.prototype.push>} items
* @returns {ReturnType<typeof Array.prototype.push>}
*/
primordials.SafeArrayPrototypePushApply = (arr, items) => {
let end = 0x10000;
if (end < items.length) {
let start = 0;
do {
ArrayPrototypePushApply(arr, ArrayPrototypeSlice(items, start, start = end));
end += 0x10000;
} while (end < items.length);
items = ArrayPrototypeSlice(items, start);
}
return ArrayPrototypePushApply(arr, items);
};

ObjectSetPrototypeOf(primordials, null);
ObjectFreeze(primordials);
10 changes: 5 additions & 5 deletions lib/internal/perf/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
ArrayPrototypeSort,
Error,
MathMax,
MathMin,
ObjectDefineProperties,
ObjectFreeze,
SafeArrayPrototypePushApply,
SafeMap,
SafeSet,
Symbol,
Expand Down Expand Up @@ -300,7 +300,7 @@ class PerformanceObserver {
maybeIncrementObserverCount(type);
if (buffered) {
const entries = filterBufferMapByNameAndType(undefined, type);
ArrayPrototypePushApply(this.#buffer, entries);
SafeArrayPrototypePushApply(this.#buffer, entries);
kPending.add(this);
if (kPending.size)
queuePending();
Expand Down Expand Up @@ -507,9 +507,9 @@ function filterBufferMapByNameAndType(name, type) {
return [];
} else {
bufferList = [];
ArrayPrototypePushApply(bufferList, markEntryBuffer);
ArrayPrototypePushApply(bufferList, measureEntryBuffer);
ArrayPrototypePushApply(bufferList, resourceTimingBuffer);
SafeArrayPrototypePushApply(bufferList, markEntryBuffer);
SafeArrayPrototypePushApply(bufferList, measureEntryBuffer);
SafeArrayPrototypePushApply(bufferList, resourceTimingBuffer);
}
if (name !== undefined) {
bufferList = ArrayPrototypeFilter(bufferList, (buffer) => buffer.name === name);
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-performance-many-marks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
require('../common');

for (let i = 0; i < 1e6; i++) {
performance.mark(`mark-${i}`);
}

performance.getEntriesByName('mark-0');
performance.clearMarks();
21 changes: 21 additions & 0 deletions test/parallel/test-primordials-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ArrayPrototypeUnshiftApply,
MathMaxApply,
MathMinApply,
SafeArrayPrototypePushApply,
StringPrototypeConcatApply,
TypedArrayOfApply,
} = require('internal/test/binding').primordials;
Expand Down Expand Up @@ -43,6 +44,26 @@ const {
assert.deepStrictEqual(arr1, expected);
}

{
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];

const expected = [...arr1, ...arr2];

assert.strictEqual(SafeArrayPrototypePushApply(arr1, arr2), expected.length);
assert.deepStrictEqual(arr1, expected);
}

{
const arr1 = [1, 2, 3];
const arr2 = Array.from({ length: 1e6 }, (_, i) => i);

const expected = [...arr1, ...arr2];

assert.strictEqual(SafeArrayPrototypePushApply(arr1, arr2), expected.length);
assert.deepStrictEqual(arr1, expected);
}

{
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
Expand Down
1 change: 1 addition & 0 deletions typings/primordials.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ declare namespace primordials {
export const RegExpPrototypeGetUnicode: UncurryGetter<typeof RegExp.prototype, "unicode">;
export const RegExpPrototypeSymbolReplace: UncurryMethod<typeof RegExp.prototype, typeof Symbol.replace>
export const RegExpPrototypeSymbolSplit: UncurryMethod<typeof RegExp.prototype, typeof Symbol.split>
export const SafeArrayPrototypePushApply: typeof ArrayPrototypePushApply;
export import Set = globalThis.Set;
export const SetLength: typeof Set.length
export const SetName: typeof Set.name
Expand Down
Loading