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

faster brand check #3720

Merged
merged 2 commits into from
Oct 11, 2024
Merged

faster brand check #3720

merged 2 commits into from
Oct 11, 2024

Conversation

tsctx
Copy link
Member

@tsctx tsctx commented Oct 11, 2024

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Symbol.hasInstance#reverting_to_default_instanceof_behavior

  • main
benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
webidl.is.FormData (ok)   7.17 ns/iter   6.91 ns  █▄
                 (6.20 ns … 150.63 ns)  14.65 ns ▁██▃▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
webidl.is.FormData (b..   8.81 ns/iter   9.03 ns █
                 (7.62 ns … 101.73 ns)  18.87 ns █▄▁▄▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁
instanceof (ok)          22.41 ns/iter  23.14 ns  █
                 (19.60 ns … 63.67 ns)  43.04 ns ▁█▂▁▄▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
instanceof (bad)         23.51 ns/iter  22.61 ns  █
                (20.53 ns … 122.12 ns)  47.97 ns ▁█▂▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

                        ┌                                            ┐
webidl.is.FormData (ok) ┤■ 7.17 ns
webidl.is.FormData (b.. ┤■■■■ 8.81 ns
        instanceof (ok) ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 22.41 ns
       instanceof (bad) ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 23.51 ns
                        └                                            ┘
  • this patch
benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
webidl.is.FormData (ok)   3.69 ns/iter   3.42 ns  █
                 (3.20 ns … 139.82 ns)   7.25 ns ▁█▁▂▂▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁
webidl.is.FormData (b.. 894.14 ps/iter 854.49 ps    █
                (756.84 ps … 47.92 ns)   1.29 ns ▁▁▇█▃▂▂▂▁▁▃▁▁▁▁▁▁▂▁▁▁
instanceof (ok)          23.62 ns/iter  24.00 ns   █
                (19.04 ns … 121.68 ns)  43.41 ns ▂▁█▄▇▁▁▂▁▁▂▁▁▁▁▁▁▁▁▁▁
instanceof (bad)         23.55 ns/iter  23.17 ns  █
                 (20.53 ns … 99.58 ns)  44.46 ns ▁█▂▁▃▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁

                        ┌                                            ┐
webidl.is.FormData (ok) ┤■■■■■ 3.69 ns
webidl.is.FormData (b.. ┤■ 894.14 ps
        instanceof (ok) ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 23.62 ns
       instanceof (bad) ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 23.55 ns
                        └                                            ┘

@tsctx tsctx force-pushed the faster-brand-check branch from 0800b1a to eed0d19 Compare October 11, 2024 11:53
@KhafraDev KhafraDev merged commit 60c07b2 into nodejs:main Oct 11, 2024
32 of 33 checks passed
@KhafraDev
Copy link
Member

I think this may have use in node core as well. I noticed when working on #3708 that instanceof called Symbol.hasInstance (if it exists), but I never realized we could call it directly, bypassing the slower paths. Nice find.

@tsctx tsctx deleted the faster-brand-check branch October 11, 2024 20:55
@tsctx
Copy link
Member Author

tsctx commented Oct 12, 2024

Unfortunately, when examined, there is no difference in the real world, but it is always faster than the original isPrototypeOf.

import { bench, run } from "mitata";

const FunctionPrototypeSymbolHasInstance = Function.call.bind(
  Function.prototype[Symbol.hasInstance]
);

function A() {
  if (!(this instanceof A)) {
    return new A();
  }

  return this;
}

function B() {
  if (!(this instanceof B)) {
    return new B();
  }

  return this;
}

class State {
  _now = -1;
  now() {
    return (this._now = (this._now + 1) & 0x7fffffff);
  }
}

const array = [];

for (let i = 0; i < 0xff; ++i) {
  array.push(Math.random() > 0.5 ? new A() : new B());
}

const state = new State();

const isPrototypeOf = Object.prototype.isPrototypeOf;
const APrototype = A.prototype;

bench("a", () => array[state.now() & 0xff] instanceof A);
bench("b", () =>
  FunctionPrototypeSymbolHasInstance(A, array[state.now() & 0xff])
);
bench("c", () => isPrototypeOf.call(A.prototype, array[state.now() & 0xff]));
bench("d", () => isPrototypeOf.call(APrototype, array[state.now() & 0xff]));

await run();
benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
a                         6.56 ns/iter   6.13 ns  █
                  (5.37 ns … 88.99 ns)  16.97 ns ▁█▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
b                         8.02 ns/iter   7.98 ns  █
                 (6.79 ns … 194.97 ns)  18.97 ns ▁█▄▂▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁
c                        14.84 ns/iter  14.18 ns  █
                 (12.82 ns … 84.62 ns)  34.67 ns ▁█▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
d                         8.46 ns/iter   8.01 ns  █
                  (7.25 ns … 82.47 ns)  21.26 ns ▁█▂▁▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁

@github-actions github-actions bot mentioned this pull request Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants