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

[ES6] Lift pure closures to outer scope #1096

Open
Ginden opened this issue May 25, 2016 · 0 comments
Open

[ES6] Lift pure closures to outer scope #1096

Ginden opened this issue May 25, 2016 · 0 comments

Comments

@Ginden
Copy link

Ginden commented May 25, 2016

Consider following code:

(function iife() {
    function foo(arr) {
        return arr.map(e=>e.someProperty).filter(Boolean)
    }
    function bar() {
          return arr.map(e=>e.someProperty).filter(e=>e===3)
    }
    return {foo, bar};
}())

This code can have e=>e.someProperty lifted to outer scope to avoid repeating code. The same optimization can be applied to constant expressions.

Pros:

  • can result in smaller code size when repeating expression can't be optimized by gzip (especially in long files)
  • can result in more memory-efficient code

Cons:

  • (e=>e) !== (e=>e) - functions are objects and they are compared by identity, not by value
  • can break code that relies on empty constructor with prototype methods.
@alexlamsl alexlamsl changed the title [Feature request] Lift pure closures to outer scope [ES6] Lift pure closures to outer scope Mar 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants