Skip to content

Commit

Permalink
perf(hdom): update event attrib checks
Browse files Browse the repository at this point in the history
- replace .indexOf("on") with array-style accessor checks
- benchmarking shows ~30% improvement
  • Loading branch information
postspectacular committed Apr 6, 2020
1 parent a6ab185 commit ab54d3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/hdom/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { SEMAPHORE } from "@thi.ng/api";
import type { IObjectOf } from "@thi.ng/api";
import { diffArray, DiffMode, diffObject } from "@thi.ng/diff";
import {
equiv as _equiv,
equivArrayLike,
equivMap,
equivObject,
equivSet
equivSet,
} from "@thi.ng/equiv";
import type { IObjectOf } from "@thi.ng/api";
import type { HDOMImplementation, HDOMOpts } from "./api";

const isArray = Array.isArray;
Expand Down Expand Up @@ -217,7 +217,7 @@ const decOffsets = (offsets: any[], j: number, idx: number) => {
* @param el - DOM element
* @param prev - previous attributes
* @param curr - current attributes
*
*
* @internal
*/
export const diffAttributes = <T>(
Expand All @@ -232,7 +232,7 @@ export const diffAttributes = <T>(
let i: number, e, edits;
for (edits = delta.edits!, i = edits.length; (i -= 2) >= 0; ) {
e = edits[i];
e.indexOf("on") === 0 && impl.removeAttribs(el, [e], prev);
e[0] === "o" && e[1] === "n" && impl.removeAttribs(el, [e], prev);
e !== "value"
? impl.setAttrib(el, e, edits[i + 1], curr)
: (val = edits[i + 1]);
Expand Down
12 changes: 6 additions & 6 deletions packages/hdom/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isArray as isa, isNotStringAndIterable as isi } from "@thi.ng/checks";
import { SVG_NS, SVG_TAGS } from "@thi.ng/hiccup";
import { css } from "@thi.ng/hiccup";
import { css, SVG_NS, SVG_TAGS } from "@thi.ng/hiccup";
import type { HDOMImplementation, HDOMOpts } from "./api";

const isArray = isa;
Expand Down Expand Up @@ -106,7 +105,7 @@ export const hydrateTree = <T>(
}
maybeInitElement(el, tree);
for (let a in attribs) {
a.indexOf("on") === 0 && impl.setAttrib(el, a, attribs[a]);
a[0] === "o" && a[1] === "n" && impl.setAttrib(el, a, attribs[a]);
}
for (let n = tree.length, i = 2; i < n; i++) {
hydrateTree(opts, impl, el, tree[i], i - 2);
Expand Down Expand Up @@ -213,7 +212,7 @@ export const setAttribs = (el: Element, attribs: any) => {
*/
export const setAttrib = (el: Element, id: string, val: any, attribs?: any) => {
if (id.startsWith("__")) return;
const isListener = id.indexOf("on") === 0;
const isListener = id[0] === "o" && id[1] === "n";
if (!isListener && typeof val === "function") {
val = val(attribs);
}
Expand Down Expand Up @@ -284,7 +283,8 @@ export const updateValueAttrib = (el: HTMLInputElement, value: any) => {
case "week":
case "month":
if ((ev = el.value) !== undefined && typeof value === "string") {
const off = value.length - (ev.length - (el.selectionStart || 0));
const off =
value.length - (ev.length - (el.selectionStart || 0));
el.value = value;
el.selectionStart = el.selectionEnd = off;
break;
Expand All @@ -297,7 +297,7 @@ export const updateValueAttrib = (el: HTMLInputElement, value: any) => {
export const removeAttribs = (el: Element, attribs: string[], prev: any) => {
for (let i = attribs.length; --i >= 0; ) {
const a = attribs[i];
if (a.indexOf("on") === 0) {
if (a[0] === "o" && a[1] === "n") {
removeListener(el, a.substr(2), prev[a]);
} else {
el.hasAttribute(a) ? el.removeAttribute(a) : ((<any>el)[a] = null);
Expand Down
6 changes: 5 additions & 1 deletion packages/hdom/src/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { isArray as isa, isNotStringAndIterable as isi, isPlainObject as iso } from "@thi.ng/checks";
import {
isArray as isa,
isNotStringAndIterable as isi,
isPlainObject as iso,
} from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
import { NO_SPANS, RE_TAG } from "@thi.ng/hiccup";
import type { HDOMOpts } from "./api";
Expand Down

0 comments on commit ab54d3c

Please sign in to comment.