summaryrefslogtreecommitdiff
path: root/ext/web/02_event.js
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2023-08-20 11:30:57 +0200
committerGitHub <noreply@github.com>2023-08-20 11:30:57 +0200
commit7847de0974889ac265f9c1ce246588f6c7ddde22 (patch)
treeaf7ded729981a2d44e5812539d7043fd29cb300c /ext/web/02_event.js
parentb9f407aef73dd5773638a4ce1690737209318be8 (diff)
perf(ext/event): optimize addEventListener options converter (#20203)
This PR optimizes `addEventListener` by replacing `webidl.createDictionaryConverter("AddEventListenerOptions", ...)` with a custom options parsing function to avoid the overhead of `webidl` methods **this PR** ``` cpu: 13th Gen Intel(R) Core(TM) i9-13900H runtime: deno 1.36.1 (x86_64-unknown-linux-gnu) benchmark time (avg) iter/s (min … max) p75 p99 p995 ---------------------------------------------------------------------------------------------------- ----------------------------- addEventListener options converter (undefined) 4.87 ns/iter 205,248,660.8 (4.7 ns … 13.18 ns) 4.91 ns 5.4 ns 5.6 ns addEventListener options converter (signal) 13.02 ns/iter 76,782,031.2 (11.74 ns … 18.84 ns) 13.08 ns 16.22 ns 16.57 ns ``` **main** ``` cpu: 13th Gen Intel(R) Core(TM) i9-13900H runtime: deno 1.36.1 (x86_64-unknown-linux-gnu) benchmark time (avg) iter/s (min … max) p75 p99 p995 ---------------------------------------------------------------------------------------------------- ----------------------------- addEventListener options converter (undefined) 108.36 ns/iter 9,228,688.6 (103.5 ns … 129.88 ns) 109.69 ns 115.61 ns 125.28 ns addEventListener options converter (signal) 134.03 ns/iter 7,460,878.1 (129.14 ns … 144.54 ns) 135.68 ns 141.13 ns 144.1 ns ``` ```js const tg = new EventTarget(); const signal = new AbortController().signal; Deno.bench("addEventListener options converter (undefined)", () => { tg.addEventListener("foo", null); // null callback to only bench options converter }); Deno.bench("addEventListener options converter (signal)", () => { tg.addEventListener("foo", null, { signal }); }); ``` Towards https://github.com/denoland/deno/issues/20167
Diffstat (limited to 'ext/web/02_event.js')
-rw-r--r--ext/web/02_event.js58
1 files changed, 19 insertions, 39 deletions
diff --git a/ext/web/02_event.js b/ext/web/02_event.js
index 4d3f23a02..2be800367 100644
--- a/ext/web/02_event.js
+++ b/ext/web/02_event.js
@@ -896,44 +896,28 @@ function getDefaultTargetData() {
};
}
-// This is lazy loaded because there is a circular dependency with AbortSignal.
-let addEventListenerOptionsConverter;
+function addEventListenerOptionsConverter(V, prefix) {
+ if (webidl.type(V) !== "Object") {
+ return { capture: !!V, once: false, passive: false };
+ }
-function lazyAddEventListenerOptionsConverter() {
- addEventListenerOptionsConverter ??= webidl.createDictionaryConverter(
- "AddEventListenerOptions",
- [
- {
- key: "capture",
- defaultValue: false,
- converter: webidl.converters.boolean,
- },
- {
- key: "passive",
- defaultValue: false,
- converter: webidl.converters.boolean,
- },
- {
- key: "once",
- defaultValue: false,
- converter: webidl.converters.boolean,
- },
- {
- key: "signal",
- converter: webidl.converters.AbortSignal,
- },
- ],
- );
-}
+ const options = {
+ capture: !!V.capture,
+ once: !!V.once,
+ passive: !!V.passive,
+ };
-webidl.converters.AddEventListenerOptions = (V, prefix, context, opts) => {
- if (webidl.type(V) !== "Object" || V === null) {
- V = { capture: Boolean(V) };
+ const signal = V.signal;
+ if (signal !== undefined) {
+ options.signal = webidl.converters.AbortSignal(
+ signal,
+ prefix,
+ "'signal' of 'AddEventListenerOptions' (Argument 3)",
+ );
}
- lazyAddEventListenerOptionsConverter();
- return addEventListenerOptionsConverter(V, prefix, context, opts);
-};
+ return options;
+}
class EventTarget {
constructor() {
@@ -952,11 +936,7 @@ class EventTarget {
webidl.requiredArguments(arguments.length, 2, prefix);
- options = webidl.converters.AddEventListenerOptions(
- options,
- prefix,
- "Argument 3",
- );
+ options = addEventListenerOptionsConverter(options, prefix);
if (callback === null) {
return;