From 20ed1ad177684b6c648a5dd545f53a7fdb79fc70 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 25 Feb 2016 23:18:14 -0500 Subject: [PATCH] Fast path for plain objects. --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6c7dd2a..7407a70 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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));