3 ms·
You could get the minified code size lower with a few tricks below. This allows the minifier to stop using the longer method names. It also has the benefit of e
by sbr464 7y ago
You could get the minified code size lower with a few tricks below. This allows the minifier to stop using the longer method names. It also has the benefit of eliminating an object property lookup which is significantly slower than accessing a variable (under a microscope of course, if called many times). Probably 5-10% savings by doing this for the Math methods.
1. Create variables for any global/builtins (setTimeout/clearTimeout, requestAnimationFrame, etc) at the top of the file.
2. Create variables for global object properties, especially if used multiple times or they have longer names (Object.assign, Object.hasOwnProperty, Math.cos, Math.sin , JSON.stringify).
3. For non globals, within functions, create variables for any object properties or methods that are called multiple times to be worth it; like in formulas where obj.width/height is called 4-5 times, the minifier can't optimize the property name.
4. For Class instance methods that don't allow creating variables for methods (You have to call the method with the class, since they operate on "this" context) but don't have anything unique about "this", you can create a variable once within a function, then just bind itself. If you are using it multiple times to be worth it.
const classMethod = ClassInstance.classMethod.bind(ClassInstance)
would be minified to
const Z = X.classMethod.bind(X)
and (in the minified code) used as Z() instead of X.classMethod()
5. In longer functions that use "this.whatever" many times, you can create a variable once for "this" (t or _this etc) and the minifier will be able to eliminate those bytes.