You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Optimize a JavaScript file for faster initial execution and parsing, by wrapping all immediately-invoked functions or likely-to-be-invoked functions in parentheses.
5
5
6
+
See the [changelog](#changelog) for recent changes.
7
+
6
8
Install
7
9
---
8
10
@@ -32,14 +34,14 @@ runIt((function (){}))
32
34
Benchmark overview
33
35
----
34
36
35
-
| Browser | Typical speed boost using `optimize-js`|
37
+
| Browser | Typical speed boost/regression using optimize-js |
36
38
| ---- | ----- |
37
-
| Chrome 52|57.09% |
38
-
| Edge 14 |28.88% |
39
-
| Firefox 48|12.49% |
40
-
| Safari 10 |6.54% |
39
+
| Chrome 55|20.63% |
40
+
| Edge 14 |13.52% |
41
+
| Firefox 50|8.26% |
42
+
| Safari 10 |-1.04% |
41
43
42
-
For benchmark details, see [benchmarks](#benchmarks).
44
+
These numbers are based on a benchmark of common JS libraries. For benchmark details, see [benchmarks](#benchmarks).
43
45
44
46
CLI
45
47
----
@@ -130,20 +132,20 @@ In all such cases, `optimize-js` wraps the function in parentheses.
130
132
Yes, `optimize-js` might add as many as two bytes (horror!) per function, which amounts to practically nil once you
131
133
take gzip into account. To prove it, here are the gzipped sizes for the libraries I use in the benchmark:
### Is `optimize-js` intended for library authors?
149
151
@@ -152,13 +154,24 @@ Sure! If you are already shipping a bundled, minified version of your library, t
152
154
Also note that because `optimize-js` optimizes for some patterns that are based on heuristics rather than _known_ eagerly-invoked
153
155
functions, it may actually hurt your performance in some cases. (See benchmarks below for examples.) Be sure to check that `optimize-js` is a help rather than a hindrance for your particular codebase, using something like:
154
156
155
-
```js
157
+
```html
158
+
<script>
156
159
var start =performance.now();
157
-
// your code goes here...
160
+
</script>
161
+
<scriptsrc="myscript.js"></script>
162
+
<script>
158
163
var end =performance.now();
159
164
console.log('took '+ (end - start) +'ms');
165
+
</script>
160
166
```
161
167
168
+
Note that the script boundaries are actually recommended, in order to truly measure the full parse/compile time.
169
+
If you'd like to avoid measuring the network overhead, you can see how we do it in [our benchmarks](https://github.com/nolanlawson/optimize-js/tree/master/benchmarks).
170
+
171
+
You may also want to check out [marky](http://github.com/nolanlawson/marky),
172
+
which allows you to easily set mark/measure points that you can visually inspect in the Dev Tools timeline to ensure that the full
173
+
compile time is being measured.
174
+
162
175
Also, be sure to test in multiple browsers! If you need an Edge VM, check out [edge.ms](http://edge.ms).
163
176
164
177
### Shouldn't this be Uglify's job?
@@ -173,7 +186,7 @@ guesses), it can be more judicious in applying the paren hack.
173
186
174
187
### Does this really work for every JavaScript engine?
175
188
176
-
Based on my tests, this optimization seems to work best for V8 (Chrome), followed by Chakra (Edge), followed by SpiderMonkey (Firefox). For JavaScriptCore (Safari) it seems to be basically a wash, although it's hard to tell because JSCore is so fast already that the numbers are tiny.
189
+
Based on my tests, this optimization seems to work best for V8 (Chrome), followed by Chakra (Edge), followed by SpiderMonkey (Firefox). For JavaScriptCore (Safari) it seems to be basically a wash, and may actually be a slight regression overall depending on your codebase. (Again, this is why it's important to actually measure on your own codebase, on the browsers you actually target!)
177
190
178
191
In the case of Chakra, [Uglify-style IIFEs are actually already optimized](https://github.com/mishoo/UglifyJS2/issues/640#issuecomment-247792319), but using `optimize-js` doesn't hurt because a
179
192
function preceded by `'('` still goes into the fast path.
@@ -183,61 +196,57 @@ Benchmarks
183
196
184
197
These tests were run using a handful of popular libraries, wrapped in `performance.now()` measurements. Each test reported the median of 251 runs. `optimize-js` commit [da51013](https://github.com/nolanlawson/optimize-js/commit/da51013) was tested. Minification was applied using `uglifyjs -mc`, Uglify 2.7.0.
185
198
186
-
You can also try [a live version of the benchmark](https://nolanlawson.github.io/optimize-js/) (note: very slow, running locally is recommended).
199
+
You can also try [a live version of the benchmark](https://nolanlawson.github.io/optimize-js/).
Note that these results may vary based on your machine, how taxed your CPU is, gremlins, etc. I ran the full suite a few times on all browsers and found these numbers to be roughly representative. However, the final "overall improvement" may vary by as much as 5%, and individual libraries can swing a bit too.
249
+
Note that these results may vary based on your machine, how taxed your CPU is, gremlins, etc. I ran the full suite a few times on all browsers and found these numbers to be roughly representative. In our test suite, we use a median of 151 runs to reduce variability.
241
250
242
251
Plugins
243
252
---
@@ -258,6 +267,9 @@ Thanks
258
267
259
268
Thanks to [@krisselden](https://github.com/krisselden), [@bmaurer](https://github.com/bmaurer), and [@pleath](https://github.com/pleath) for explaining these concepts in the various GitHub issues. Thanks also to [astexplorer](https://github.com/fkling/astexplorer), [acorn](https://github.com/ternjs/acorn), and [magic-string](https://www.npmjs.com/package/magic-string) for making the implementation so easy.
260
269
270
+
Thanks to [Sasha Aickin](https://github.com/aickin) for generous contributions to improving this library (especially in v1.0.3)
271
+
and prodding me to improve the accuracy of the benchmarks.
0 commit comments