Skip to content

Commit

Permalink
Fast path for plain objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Feb 26, 2016
1 parent fab2d8a commit 20ed1ad
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ function toTable(value) {
if (value && typeof value === "object") {
var keys = Object.keys(value);

if (Array.isArray(value)) {
if (isPlainObject(value)) {
result = {};

} else if (Array.isArray(value)) {
result = Array(value.length);
var len = value.length;
if (len > keys.length) {
Expand Down Expand Up @@ -136,6 +139,17 @@ function toTable(value) {
return table;
}

function isPlainObject(value) {
var isObject = value && typeof value === "object";
if (isObject) {
var proto = Object.getPrototypeOf
? Object.getPrototypeOf(value)
: value.__proto__;
return proto === Object.prototype;
}
return false;
}

exports.decode = exports.parse =
function decode(encoding) {
return fromTable(JSON.parse(encoding));
Expand Down

0 comments on commit 20ed1ad

Please sign in to comment.