Skip to content

Commit

Permalink
Use a Map to accelerate seen object lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jan 29, 2016
1 parent db1e6e7 commit d75d0c9
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
exports.encode = exports.stringify = function (value) {
var values = [value];
function encode(value) {
return JSON.stringify(tabulate(value));
}

function tabulate(value) {
var values = [];
var table = [];
var indexMap = typeof Map === "function" && new Map;

function getIndex(value) {
var index;

if (indexMap) {
// If we have Map, use it instead of values.indexOf to accelerate
// object lookups.
index = indexMap.get(value);
if (typeof index === "undefined") {
index = values.push(value) - 1;
indexMap.set(value, index);
}
} else {
index = values.indexOf(value);
if (index < 0) {
index = values.push(value) - 1;
}
}

return index;
}

// Assign the root value to values[0].
getIndex(value);

for (var v = 0; v < values.length; ++v) {
value = values[v];

if (value && typeof value === "object") {
var copy = table[v] = Array.isArray(value) ? [] : {};
Object.keys(value).forEach(function (key) {
var child = value[key];
var index = values.indexOf(child);
if (index < 0) {
index = values.push(child) - 1;
}
copy[key] = index;
copy[key] = getIndex(value[key]);
});
} else {
table[v] = value;
}
}

return JSON.stringify(table);
};
return table;
}

exports.decode = exports.parse = function (encoding) {
function decode(encoding) {
var table = JSON.parse(encoding);

table.forEach(function (entry) {
Expand All @@ -34,4 +59,7 @@ exports.decode = exports.parse = function (encoding) {
});

return table[0];
};
}

exports.encode = exports.stringify = encode;
exports.decode = exports.parse = decode;

0 comments on commit d75d0c9

Please sign in to comment.