Skip to content

Commit

Permalink
Move to modular lodash
Browse files Browse the repository at this point in the history
 * Removed use of `chain` as it required (effectively) all of lodash
 * Switched to native Function.bind for the same reason.

With similar change to graphlib,

 * dagre.js: 636K => 317K
 * dagre.min.js: 554K => 278K
  • Loading branch information
mwaldstein committed Jul 10, 2018
1 parent 7e050f8 commit dce716e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
31 changes: 29 additions & 2 deletions lib/lodash.js
@@ -1,10 +1,37 @@
/* global window */

var lodash;
var lodash = {};

if (typeof require === "function") {
try {
lodash = require("lodash");
lodash = {
cloneDeep: require("lodash/cloneDeep"),
constant: require("lodash/constant"),
defaults: require("lodash/defaults"),
each: require("lodash/each"),
filter: require("lodash/filter"),
find: require("lodash/find"),
flatten: require("lodash/flatten"),
forEach: require("lodash/forEach"),
forIn: require("lodash/forIn"),
has: require("lodash/has"),
isUndefined: require("lodash/isUndefined"),
last: require("lodash/last"),
map: require("lodash/map"),
mapValues: require("lodash/mapValues"),
max: require("lodash/max"),
merge: require("lodash/merge"),
min: require("lodash/min"),
minBy: require("lodash/minBy"),
now: require("lodash/now"),
pick: require("lodash/pick"),
range: require("lodash/range"),
reduce: require("lodash/reduce"),
sortBy: require("lodash/sortBy"),
uniqueId: require("lodash/uniqueId"),
values: require("lodash/values"),
zipObject: require("lodash/zipObject"),
};
} catch (e) {}
}

Expand Down
7 changes: 2 additions & 5 deletions lib/order/cross-count.js
Expand Up @@ -35,12 +35,9 @@ function twoLayerCrossCount(g, northLayer, southLayer) {
var southPos = _.zipObject(southLayer,
_.map(southLayer, function (v, i) { return i; }));
var southEntries = _.flatten(_.map(northLayer, function(v) {
return _.chain(g.outEdges(v))
.map(function(e) {
return _.sortBy(_.map(g.outEdges(v), function(e) {
return { pos: southPos[e.w], weight: g.edge(e).weight };
})
.sortBy("pos")
.value();
}), "pos");
}), true);

// Build the accumulator tree
Expand Down
11 changes: 5 additions & 6 deletions lib/order/resolve-conflicts.js
Expand Up @@ -93,12 +93,11 @@ function doResolveConflicts(sourceSet) {
_.forEach(entry.out, handleOut(entry));
}

return _.chain(entries)
.filter(function(entry) { return !entry.merged; })
.map(function(entry) {
return _.pick(entry, ["vs", "i", "barycenter", "weight"]);
})
.value();
return _.map(_.filter(entries, function(entry) { return !entry.merged; }),
function(entry) {
return _.pick(entry, ["vs", "i", "barycenter", "weight"]);
});

}

function mergeEntries(target, source) {
Expand Down
6 changes: 3 additions & 3 deletions lib/position/bk.js
Expand Up @@ -250,8 +250,8 @@ function horizontalCompaction(g, layering, root, align, reverseSep) {
}
}

iterate(pass1, _.bind(blockG.predecessors, blockG));
iterate(pass2, _.bind(blockG.successors, blockG));
iterate(pass1, blockG.predecessors.bind(blockG));
iterate(pass2, blockG.successors.bind(blockG));

// Assign x coordinates to all nodes
_.forEach(align, function(v) {
Expand Down Expand Up @@ -359,7 +359,7 @@ function positionX(g) {
});
}

var neighborFn = _.bind(vert === "u" ? g.predecessors : g.successors, g);
var neighborFn = (vert === "u" ? g.predecessors : g.successors).bind(g);
var align = verticalAlignment(g, adjustedLayering, conflicts, neighborFn);
var xs = horizontalCompaction(g, adjustedLayering,
align.root, align.align,
Expand Down

0 comments on commit dce716e

Please sign in to comment.