json-refs icon indicating copy to clipboard operation
json-refs copied to clipboard

Load only those lodash functions as necessary

Open mhamann opened this issue 8 years ago • 4 comments
trafficstars

Helps a good bit with overall package size.

mhamann avatar Nov 16 '17 18:11 mhamann

I use to do this and I think I removed it out of laziness.

whitlockjc avatar Nov 20 '17 16:11 whitlockjc

Also, when you get the code changes in, can you run node ./node_modules/gulp/bin/gulp.js so that the browser binaries are rebuilt?

whitlockjc avatar Nov 20 '17 16:11 whitlockjc

@whitlockjc ok, no problem. will update the PR shortly...

mhamann avatar Nov 20 '17 19:11 mhamann

I'm not seeing the package size improvements, unless you're talking about from the NPM perspective. When I build the browser binaries, here are their respective sizes:

  • Require all of lodash *
36      browser/json-refs-min.js
136     browser/json-refs-standalone-min.js
1908    browser/json-refs-standalone.js
320     browser/json-refs.js
  • Cherry-pick lodash *
64      browser/json-refs-min.js
164     browser/json-refs-standalone-min.js
2128    browser/json-refs-standalone.js
540     browser/json-refs.js

Below is the diff of the changes as I would have them in the PR:

diff --git a/index.js b/index.js
index 0265523..f40a263 100644
--- a/index.js
+++ b/index.js
@@ -32,7 +32,18 @@
  */
 
 // Cherry-pick lodash components to help with size
-var _ = require('lodash');
+var _ = {
+  cloneDeep: require('lodash/cloneDeep'),
+  forOwn: require('lodash/forOwn'),
+  isArray: require('lodash/isArray'),
+  isBoolean: require('lodash/isBoolean'),
+  isError: require('lodash/isError'),
+  isFunction: require('lodash/isFunction'),
+  isObject: require('lodash/isObject'),
+  isPlainObject: require('lodash/isPlainObject'),
+  isString: require('lodash/isString'),
+  isUndefined: require('lodash/isUndefined')
+};
 var gl = require('graphlib');
 var path = require('path');
 var PathLoader = require('path-loader');
@@ -58,7 +69,7 @@ function combineQueryParams (qs1, qs2) {
   var combined = {};
 
   function mergeQueryParams (obj) {
-    _.each(obj, function (val, key) {
+    _.forOwn(obj, function (val, key) {
       combined[key] = val;
     });
   }
@@ -326,7 +337,7 @@ function buildRefModel (document, options, metadata) {
     refs = findRefs(document, options);
 
     // Iterate over the references and process
-    _.each(refs, function (refDetails, refPtr) {
+    _.forOwn(refs, function (refDetails, refPtr) {
       var refKey = makeAbsolute(options.location) + refPtr;
       var refdKey = refDetails.refdId = makeAbsolute(isRemote(refDetails) ?
                                                        combineURIs(relativeBase, refDetails.uri) :
@@ -1173,8 +1184,8 @@ function resolveRefs (obj, options) {
       });
 
       // Add edges
-      _.each(results.deps, function (props, node) {
-        _.each(props, function (dep) {
+      _.forOwn(results.deps, function (props, node) {
+        _.forOwn(props, function (dep) {
           depGraph.setEdge(node, dep);
         });
       });
@@ -1191,8 +1202,8 @@ function resolveRefs (obj, options) {
       });
 
       // Process circulars
-      _.each(results.deps, function (props, node) {
-        _.each(props, function (dep, prop) {
+      _.forOwn(results.deps, function (props, node) {
+        _.forOwn(props, function (dep, prop) {
           var isCircular = false;
           var refPtr = node + prop.slice(1);
           var refDetails = results.refs[node + prop.slice(1)];
@@ -1237,13 +1248,13 @@ function resolveRefs (obj, options) {
       });
 
       // Resolve the references in reverse order since the current order is top-down
-      _.each(Object.keys(results.deps).reverse(), function (parentPtr) {
+      _.forOwn(Object.keys(results.deps).reverse(), function (parentPtr) {
         var deps = results.deps[parentPtr];
         var pPtrParts = parentPtr.split('#');
         var pDocument = results.docs[pPtrParts[0]];
         var pPtrPath = pathFromPtr(pPtrParts[1]);
 
-        _.each(deps, function (dep, prop) {
+        _.forOwn(deps, function (dep, prop) {
           var depParts = dep.split('#');
           var dDocument = results.docs[depParts[0]];
           var dPtrPath = pPtrPath.concat(pathFromPtr(prop));
@@ -1329,7 +1340,7 @@ function resolveRefs (obj, options) {
       });
 
       // Sanitize the reference details
-      _.each(results.refs, function (refDetails) {
+      _.forOwn(results.refs, function (refDetails) {
         // Delete the reference id used for dependency tracking and circular identification
         delete refDetails.refdId;
       });

whitlockjc avatar Jan 03 '18 17:01 whitlockjc