question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slow performance using getter references other getter

See original GitHub issue

Version

3.1.2

Reproduction link

https://jsfiddle.net/tbetous/y4m0keav/53/

Steps to reproduce

1 - Write a getter myGetter that use another getter otherGetter by using getters argument

myGetter: (state, getters) => {
      console.time('myGetter')
      const t0 = performance.now();
      for(let i = 0; i < 100; i  ) {
        const foo = getters.otherGetter
      }
      console.timeEnd('myGetter')
      const t1 = performance.now();
      return t1 - t0
    },

2 - Write a getter myGetterOptimized that use another getter otherGetter by destructuring getters

myGetterOptimized: (state, {otherGetter}) => {
      console.time('myGetterOptimized')
      const t0 = performance.now();
      for(let i = 0; i < 100; i  ) {
        const foo = otherGetter
      }
      console.timeEnd('myGetterOptimized')
      const t1 = performance.now();
      return t1 - t0
    },

3 - Compare time execution

What is expected?

Time execution should be the same.

What is actually happening?

Time execution is different :

  • getterOptimized : 0.21999999989930075
  • getterNotOptimized : 494.8850000000675

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tbetouscommented, Jan 27, 2020

Thanks a lot @kiaking ! You answered to my question ! I guess I can close the issue.

0reactions
Kasheftincommented, Apr 28, 2022

@cuebit If we track the getter’s calculation, we find that it’s calculated only once no matter if called in a cycle or before the cycle. https://jsfiddle.net/kasheftin/9oh3svn2/ - here’s the slightly updated version, where getter calc included into timeline. It shows the same difference. I would say that the real reason of this huge difference is that any vuex getter provides a Proxy, calling the getter requests Proxy.get method, and the last has some inner logic like dependency tracking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ - Getters and Setters. Is there performance overhead?
Setters and getters have a performance overhead when not optimized out. They are almost always optimized out on compilers that do link time...
Read more >
782913 - Using property getters/setters is dramatically slower ...
Using property getters/setters is dramatically slower than calling the getters/setters directly.
Read more >
Effective Dart: Design
When a member produces a result without any side effects, it should usually be a getter or a method with a noun phrase...
Read more >
direct access vs. setter-getter performance - Community
Getters and setters are essentially sugar-coated wrappers for methods. So it's going to be slower than directly accessing something. Moreover, ...
Read more >
Getters And Setters In Java: Common Mistakes, And ... - Xperti
Mistake 2: Assigning object references directly in the setter. Mistake 3: Returning the object reference directly in the getter. Mistake 4: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found