Giter VIP home page Giter VIP logo

Comments (8)

bertho-zero avatar bertho-zero commented on May 31, 2024

It's the reflection between context.arguments and the named parameters that slows everything down.
If I replace this piece of code with only context.arguments = args; then it works without additional slowness.

Object.defineProperty(context, 'arguments', {
enumerable: true,
get (this: HookContext<T>) {
const result: any = [];
params.forEach((param, index) => {
const name = typeof param === 'string' ? param : param[0];
Object.defineProperty(result, index, {
enumerable: true,
configurable: true,
get: () => this[name],
set: (value) => {
this[name] = value;
if (result[index] !== this[name]) {
result[index] = value;
}
}
});
this[name] = result[index];
});
return result;
}
});

From:

Baseline 20
Empty hooks 195
Single simple hook 268
Single hook and withParams 910
Single hook, multiple withParams 1531
Single hook, multiple withParams (no named) 250

To

Baseline 16
Empty hooks 180
Single simple hook 230
Single hook and withParams 232
Single hook, multiple withParams 365
Single hook, multiple withParams (no named) 221

from hooks.

daffl avatar daffl commented on May 31, 2024

That was my suspicion, too. Basically the Object.defineProperty should happen once when the function is wrapped instead of every time it is run which makes a huge difference. I have some ideas on a more performant hook context initializer that does this, I will share it here when I gave it a test run.

from hooks.

bertho-zero avatar bertho-zero commented on May 31, 2024

I wonder if a Proxy (https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Proxy) would be the solution?

from hooks.

bertho-zero avatar bertho-zero commented on May 31, 2024

Using a Proxy gives better results:

Baseline 15
Empty hooks 181
Single simple hook 202
Single hook and withParams 409
Single hook, multiple withParams 546
Single hook, multiple withParams (no named) 214

I opened a PR with that: #29

from hooks.

bertho-zero avatar bertho-zero commented on May 31, 2024

The other thing that slows down a lot is the Object.seal(context.arguments)

With Object.seal:

Baseline 14
Empty hooks 174
Single simple hook 197
Single hook and withParams 476
Single hook, multiple withParams 462
Single hook, multiple withParams (no named) 177

Without Object.seal:

Baseline 16
Empty hooks 130
Single simple hook 124
Single hook and withParams 283
Single hook, multiple withParams 292
Single hook, multiple withParams (no named) 128

jsPerf: https://jsperf.com/object-seal-freeze

from hooks.

bertho-zero avatar bertho-zero commented on May 31, 2024

My final result:

Baseline 14
Empty hooks 122
Single simple hook 128
Single hook and withParams 285
Single hook, multiple withParams 277
Single hook, multiple withParams (no named) 116

from hooks.

daffl avatar daffl commented on May 31, 2024

I started a branch in https://github.com/feathersjs/hooks/tree/dl/hook-manager to add to #22 and did some refactoring to see if there is room for improvements using a custom context class where things like argument names are defined once on the protoype instead of every function call. These are the results I am seeing with the benchmark at the moment:

Baseline 14
Empty hooks 120
Single simple hook 147
Single hook and withParams 143

I haven't had a chance to see if it plays well with feathersjs/feathers#1798 yet though.

from hooks.

bertho-zero avatar bertho-zero commented on May 31, 2024

Amazing, haven't you made the changes on decorator.js or is it an oversight?
The other thing is that the enumerable props and params are not really enumerable with getContextClass.

from hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.