Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v18.x backport] lib: improve AbortController creation duration #46078

Closed
Closed
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
9 changes: 3 additions & 6 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,12 @@ function validateAbortController(obj) {
}

class AbortController {
constructor() {
this[kSignal] = createAbortSignal();
}

/**
* @type {AbortSignal}
*/
get signal() {
validateAbortController(this);
this[kSignal] ??= createAbortSignal();
return this[kSignal];
}

Expand All @@ -335,7 +332,7 @@ class AbortController {
*/
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
validateAbortController(this);
abortSignal(this[kSignal], reason);
abortSignal(this[kSignal] ??= createAbortSignal(), reason);
}

[customInspectSymbol](depth, options) {
Expand All @@ -346,7 +343,7 @@ class AbortController {

static [kMakeTransferable]() {
const controller = new AbortController();
controller[kSignal] = transferableAbortSignal(controller[kSignal]);
controller[kSignal] = createAbortSignal({ transferable: true });
return controller;
}
}
Expand Down
13 changes: 4 additions & 9 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ const {
decodeUTF8,
} = internalBinding('buffer');

let Buffer;
function lazyBuffer() {
if (Buffer === undefined)
Buffer = require('buffer').Buffer;
return Buffer;
}
const { Buffer } = require('buffer');

function validateEncoder(obj) {
if (obj == null || obj[kEncoder] !== true)
Expand Down Expand Up @@ -499,14 +494,14 @@ function makeTextDecoderJS() {
validateDecoder(this);
if (isAnyArrayBuffer(input)) {
try {
input = lazyBuffer().from(input);
input = Buffer.from(input);
} catch {
input = empty;
}
} else if (isArrayBufferView(input)) {
try {
input = lazyBuffer().from(input.buffer, input.byteOffset,
input.byteLength);
input = Buffer.from(input.buffer, input.byteOffset,
input.byteLength);
} catch {
input = empty;
}
Expand Down