1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/cli/tests/unit/event_test.ts
Marcos Casagrande ec63b36994
perf(ext/event): optimize Event constructor (#20181)
This PR optimizes `Event` constructor

- ~Added a fast path for empty `eventInitDict`~ Removed `EventInit`
dictionary converter
- Don't make `isTrusted` a
[LegacyUnforgeable](https://webidl.spec.whatwg.org/#LegacyUnforgeable)
property. Doing so makes it non-spec compliant but calling
`Object/Reflect.defineProperty` on the constructor is a big bottleneck.
Node did the same a few months ago
https://github.com/nodejs/node/pull/46974. In my opinion, the
performance gains are worth deviating from the spec for a
browser-related property.

**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
------------------------------------------------------------------------------- -----------------------------
event constructor no init      36.69 ns/iter  27,257,504.6   (33.36 ns … 42.45 ns)  37.71 ns  39.61 ns  40.07 ns
event constructor               36.7 ns/iter  27,246,776.6   (33.35 ns … 56.03 ns)  37.73 ns  40.14 ns  41.74 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
------------------------------------------------------------------------------- -----------------------------
event constructor no init     380.48 ns/iter   2,628,275.8 (366.66 ns … 399.39 ns) 384.58 ns 398.27 ns 399.39 ns
event constructor             480.33 ns/iter   2,081,882.6 (466.67 ns … 503.47 ns) 484.27 ns 501.28 ns 503.47 ns
```

```js
Deno.bench("event constructor no init", () => {
  const event = new Event("foo");
});

Deno.bench("event constructor", () => {
  const event = new Event("foo", { bubbles: true, cancelable: false });
});
```

towards https://github.com/denoland/deno/issues/20167
2023-08-17 10:35:18 +02:00

143 lines
5.2 KiB
TypeScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertStringIncludes } from "./test_util.ts";
Deno.test(function eventInitializedWithType() {
const type = "click";
const event = new Event(type);
assertEquals(event.isTrusted, false);
assertEquals(event.target, null);
assertEquals(event.currentTarget, null);
assertEquals(event.type, "click");
assertEquals(event.bubbles, false);
assertEquals(event.cancelable, false);
});
Deno.test(function eventInitializedWithTypeAndDict() {
const init = "submit";
const eventInit = { bubbles: true, cancelable: true } as EventInit;
const event = new Event(init, eventInit);
assertEquals(event.isTrusted, false);
assertEquals(event.target, null);
assertEquals(event.currentTarget, null);
assertEquals(event.type, "submit");
assertEquals(event.bubbles, true);
assertEquals(event.cancelable, true);
});
Deno.test(function eventComposedPathSuccess() {
const type = "click";
const event = new Event(type);
const composedPath = event.composedPath();
assertEquals(composedPath, []);
});
Deno.test(function eventStopPropagationSuccess() {
const type = "click";
const event = new Event(type);
assertEquals(event.cancelBubble, false);
event.stopPropagation();
assertEquals(event.cancelBubble, true);
});
Deno.test(function eventStopImmediatePropagationSuccess() {
const type = "click";
const event = new Event(type);
assertEquals(event.cancelBubble, false);
event.stopImmediatePropagation();
assertEquals(event.cancelBubble, true);
});
Deno.test(function eventPreventDefaultSuccess() {
const type = "click";
const event = new Event(type);
assertEquals(event.defaultPrevented, false);
event.preventDefault();
assertEquals(event.defaultPrevented, false);
const eventInit = { bubbles: true, cancelable: true } as EventInit;
const cancelableEvent = new Event(type, eventInit);
assertEquals(cancelableEvent.defaultPrevented, false);
cancelableEvent.preventDefault();
assertEquals(cancelableEvent.defaultPrevented, true);
});
Deno.test(function eventInitializedWithNonStringType() {
// deno-lint-ignore no-explicit-any
const type: any = undefined;
const event = new Event(type);
assertEquals(event.isTrusted, false);
assertEquals(event.target, null);
assertEquals(event.currentTarget, null);
assertEquals(event.type, "undefined");
assertEquals(event.bubbles, false);
assertEquals(event.cancelable, false);
});
Deno.test(function eventInspectOutput() {
// deno-lint-ignore no-explicit-any
const cases: Array<[any, (event: any) => string]> = [
[
new Event("test"),
(event: Event) =>
`Event {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "test"\n}`,
],
[
new ErrorEvent("error"),
(event: Event) =>
`ErrorEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "error",\n message: "",\n filename: "",\n lineno: 0,\n colno: 0,\n error: undefined\n}`,
],
[
new CloseEvent("close"),
(event: Event) =>
`CloseEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "close",\n wasClean: false,\n code: 0,\n reason: ""\n}`,
],
[
new CustomEvent("custom"),
(event: Event) =>
`CustomEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "custom",\n detail: undefined\n}`,
],
[
new ProgressEvent("progress"),
(event: Event) =>
`ProgressEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "progress",\n lengthComputable: false,\n loaded: 0,\n total: 0\n}`,
],
];
for (const [event, outputProvider] of cases) {
assertEquals(Deno.inspect(event), outputProvider(event));
}
});
Deno.test(function inspectEvent() {
// has a customInspect implementation that previously would throw on a getter
assertEquals(
Deno.inspect(Event.prototype),
`Event {
bubbles: [Getter],
cancelable: [Getter],
composed: [Getter],
currentTarget: [Getter],
defaultPrevented: [Getter],
eventPhase: [Getter],
srcElement: [Getter/Setter],
target: [Getter],
returnValue: [Getter/Setter],
timeStamp: [Getter],
type: [Getter]
}`,
);
// ensure this still works
assertStringIncludes(
Deno.inspect(new Event("test")),
// check a substring because one property is a timestamp
`Event {\n bubbles: false,\n cancelable: false,`,
);
});