From 8c15f10b8c0c7b5ae4df74d28f7e7dc2a86bc430 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Tue, 17 Dec 2019 10:34:12 +0800 Subject: [PATCH 1/2] don't call String.split() for simple keys --- lib/util.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/util.js b/lib/util.js index 9929a15d..9df74b12 100644 --- a/lib/util.js +++ b/lib/util.js @@ -27,12 +27,14 @@ exports.getProp = (obj, key) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); + if (key.indexOf('.') < 0) { + return obj[key]; + } + const token = extractPropKey(key); - let result = obj[token.shift()]; + let result = obj; const len = token.length; - if (!len) return result; - for (let i = 0; i < len; i++) { result = result[token[i]]; } @@ -44,15 +46,15 @@ exports.setProp = (obj, key, value) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); + if (key.indexOf('.') < 0) { + obj[key] = value; + return; + } + const token = extractPropKey(key); const lastKey = token.pop(); const len = token.length; - if (!len) { - obj[lastKey] = value; - return; - } - let cursor = obj; for (let i = 0; i < len; i++) { @@ -68,15 +70,15 @@ exports.delProp = (obj, key) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); + if (key.indexOf('.') < 0) { + delete obj[key]; + return; + } + const token = extractPropKey(key); const lastKey = token.pop(); const len = token.length; - if (!len) { - delete obj[lastKey]; - return; - } - let cursor = obj; for (let i = 0; i < len; i++) { @@ -97,15 +99,15 @@ exports.setGetter = (obj, key, fn) => { if (!key) throw new TypeError('key is required!'); if (typeof fn !== 'function') throw new TypeError('fn must be a function!'); + if (key.indexOf('.') < 0) { + obj.__defineGetter__(key, fn); + return; + } + const token = extractPropKey(key); const lastKey = token.pop(); const len = token.length; - if (!len) { - obj.__defineGetter__(lastKey, fn); - return; - } - let cursor = obj; for (let i = 0; i < len; i++) { From ca5e6f837ca393f46dadd2ba3c114bd485db7bd3 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Tue, 17 Dec 2019 23:26:23 +0800 Subject: [PATCH 2/2] Use .includes(), rather than .indexOf(), when checking for existence. --- lib/util.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/util.js b/lib/util.js index 9df74b12..596e2acd 100644 --- a/lib/util.js +++ b/lib/util.js @@ -27,7 +27,7 @@ exports.getProp = (obj, key) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); - if (key.indexOf('.') < 0) { + if (!key.includes('.')) { return obj[key]; } @@ -46,7 +46,7 @@ exports.setProp = (obj, key, value) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); - if (key.indexOf('.') < 0) { + if (!key.includes('.')) { obj[key] = value; return; } @@ -70,7 +70,7 @@ exports.delProp = (obj, key) => { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); - if (key.indexOf('.') < 0) { + if (!key.includes('.')) { delete obj[key]; return; } @@ -99,7 +99,7 @@ exports.setGetter = (obj, key, fn) => { if (!key) throw new TypeError('key is required!'); if (typeof fn !== 'function') throw new TypeError('fn must be a function!'); - if (key.indexOf('.') < 0) { + if (!key.includes('.')) { obj.__defineGetter__(key, fn); return; }