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

fix(util): improve performance of classToInvokable #10534

Merged
merged 1 commit into from Mar 11, 2019

Conversation

SimonSchick
Copy link
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Script:

'use strict';

class A {
  constructor(a, b) {
    this.rng = Math.random();
    this.a = a;
    this.b = b;
  }
}

A.a = 1;
A.b = 2;

function classToInvokable(Class) {
  return new Proxy(Class, {
    apply(Target, thisArg, args) {
      return new Target(...args);
    },
    // NEW
    construct(Target, args) {
      return new Target(...args);
    },
    get(target, p) {
      return target[p];
    }
  });
}

const B = classToInvokable(A);

console.log(new A(1, 2), new B, new B(1, 2), B(1, 2));

console.time('class');
for (let i = 0;i<0xFFFFF;++i) {
  const a = new A();
}
console.timeEnd('class');


console.time('proxy new');
for (let i = 0;i<0xFFFFF;++i) {
  const b = new B();
}
console.timeEnd('proxy new');

console.time('proxy invoke');
for (let i = 0;i<0xFFFFF;++i) {
  const b = B();
}
console.timeEnd('proxy invoke');

const a = new A();
console.time('class');
for (let i = 0;i<0xFFFFF;++i) {
  A.a + A.b;
}
console.timeEnd('class');


const b = new B();
console.time('proxy');
for (let i = 0;i<0xFFFFF;++i) {
  B.a + B.b;
}
console.timeEnd('proxy');
A { rng: 0.583534638354642, a: 1, b: 2 } A { rng: 0.6534978634885156, a: undefined, b: undefined } A { rng: 0.9860787273348626, a: 1, b: 2 } A { rng: 0.9952360726330047, a: 1, b: 2 }
class: 17.758ms
proxy new: 2372.765ms
proxy invoke: 92.412ms
class: 2.418ms
proxy: 119.242ms

 === WITH TRAPS ===

➜  sequelize git:(chore/performance) ✗ node perf.js
A { rng: 0.7856633966445696, a: 1, b: 2 } A { rng: 0.9549997856154535, a: undefined, b: undefined } A { rng: 0.14584827492246366, a: 1, b: 2 } A { rng: 0.9905286808234037, a: 1, b: 2 }
class: 17.300ms
proxy new: 63.847ms
proxy invoke: 60.784ms
class: 2.299ms
proxy: 76.098ms

As you can see the Proxy is dramatically slower without the traps.

@codecov
Copy link

codecov bot commented Mar 11, 2019

Codecov Report

Merging #10534 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #10534      +/-   ##
==========================================
+ Coverage   96.16%   96.16%   +<.01%     
==========================================
  Files          92       92              
  Lines        9005     9007       +2     
==========================================
+ Hits         8660     8662       +2     
  Misses        345      345
Impacted Files Coverage Δ
lib/utils/classToInvokable.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a26193a...0c0f9d3. Read the comment docs.

construct(Target, args) {
return new Target(...args);
},
get(target, p) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this get trap? Isn't default get implementation is same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea but it also appears to improve performance for some reason 😛

@sushantdhiman sushantdhiman merged commit f862e6b into sequelize:master Mar 11, 2019
@SimonSchick SimonSchick deleted the chore/performance branch March 11, 2019 12:54
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.

None yet

2 participants