feathersjs / hooks

Async middleware for JavaScript and TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance optimizations

daffl opened this issue · comments

One of the beta testers reported a noticeable performance difference in the latest version vs the current Feathers hooks. I ran a simple benchmark with different configurations and got the following results:

Baseline 11
Empty hooks 115
Single simple hook 138
Single hook and withParams 813
Single hook, multiple withParams 1331

Using the following test code:

  it.only('benchmark', async () => {
    const getRuntime = async (callback: () => Promise<any>) => {
      const start = Date.now();
    
      for (let i = 0; i < 100000; i++) {
        await callback();
      }
    
      return Date.now() - start;
    }
    const hello = async (name: string, _params: any = {}) => {
      return `Hello ${name}`;
    };
    console.log('Baseline', await getRuntime(() => hello('Dave')));

    const hookHello1 = hooks(hello, []);
    console.log('Empty hooks', await getRuntime(() => hookHello1('Dave')));

    const hookHello2 = hooks(hello, [
      async (_ctx: HookContext, next: NextFunction) => {
        await next();
      }
    ]);

    console.log('Single simple hook', await getRuntime(() => hookHello2('Dave')));

    const hookHello3 = hooks(hello, {
      middleware: [
        async (_ctx: HookContext, next: NextFunction) => {
          await next();
        }
      ],
      context: [withParams('name')]
    });

    console.log('Single hook and withParams', await getRuntime(() => hookHello3('Dave')));


    const hookHello4 = hooks(hello, {
      middleware: [
        async (_ctx: HookContext, next: NextFunction) => {
          await next();
        }
      ],
      context: [withParams(), withParams('name'), withParams()]
    });

    console.log('Single hook, multiple withParams', await getRuntime(() => hookHello4('Dave')));
  });

While there will be always some kind of overhead of course, I think runtime could be improved by wrapping the context updaters into a single function instead of looping every time.

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

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.

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

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

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

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.

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.