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

Unwrapping redundant IIFEs #22

Closed
Rich-Harris opened this issue Jan 30, 2014 · 7 comments
Closed

Unwrapping redundant IIFEs #22

Rich-Harris opened this issue Jan 30, 2014 · 7 comments
Assignees

Comments

@Rich-Harris
Copy link
Contributor

In my library I have many small modules that are used often, like this:

utils/isNumeric.js

define( function () {

  'use strict';

  // http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
  return function ( thing ) {
    return !isNaN( parseFloat( thing ) ) && isFinite( thing );
  };

});

After it's been through r.js and amdclean, it looks like this:

var utils_isNumeric = function () {

        return function (thing) {
            return !isNaN(parseFloat(thing)) && isFinite(thing);
        };
    }();

In this case, there are no dependencies, and no variable declarations - just a single return statement. So it could be rewritten like this:

var utils_isNumeric = function (thing) {
  return !isNaN(parseFloat(thing)) && isFinite(thing);
};

I think this is probably quite a common case in libraries that use AMD for internal code organisation. In fact the example on http://gregfranko.com/amdclean/ is another one:

// AMD
define('example', [], function() {
  return 'Convert AMD code to standard JavaScript';
});

// Standard JS
var example = function () {
        return 'Convert AMD code to standard JavaScript';
    }();

// but could be
var example = 'Convert AMD code to standard JavaScript';

I was wondering if it's possible to identify these cases (i.e. no dependencies, definition body is a single return statement) and discard the outer IIFE when they're encountered?

@gfranko
Copy link
Owner

gfranko commented Jan 30, 2014

This is definitely possible and shouldn't be too difficult. Basically, if there is only one AST object (e.g function) in the callback function body and no dependencies, then convert the module to the optimized format. I'll try to get this done in the next day or two.

@Rich-Harris
Copy link
Contributor Author

Brilliant!

@gfranko
Copy link
Owner

gfranko commented Jan 30, 2014

If you can think of any other optimizations, definitely let me know. I think the project is stable enough now that we can now focus on more minor optimizations like this.

gfranko added a commit that referenced this issue Feb 9, 2014
gfranko added a commit that referenced this issue Feb 9, 2014
* dev:
  Last minute README typo fixes
  Officially releasing v0.7.0
  Updated the README with a few more examples
  Made a few other updates
  Releasing v0.7.0
  Re: #22 - Added optimization techniques
  Cleaned up createAst() code
  Re: #23 - Comments are no longer stripped by default
@gfranko
Copy link
Owner

gfranko commented Feb 9, 2014

These optimizations have now been incorporated with the 0.7.0 release! Check out the release notes for more details.

Thanks for pointing out these optimizations Rich!

@gfranko gfranko closed this as completed Feb 9, 2014
@Rich-Harris
Copy link
Contributor Author

Excellent! Many thanks. It's been great to watch amdclean get better and better - developing a library as a collection of AMD modules is such a pleasure (testing without building, etc) and it's only practical to do so because of amdclean. Great work.

@gfranko
Copy link
Owner

gfranko commented Feb 9, 2014

You and I like to build libraries in a similar way. Love using AMD for it.

And I appreciate all the kind words, but you should give yourself credit too. AMDClean wouldn't be where it is now without you. Hopefully once I find some free time I can contribute to some of your awesome projects, like Ractive.js

@Rich-Harris
Copy link
Contributor Author

That'd be great :)

@gfranko gfranko self-assigned this Feb 19, 2014
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

2 participants