From 40de39353946a5fcf5ca1ac6bd82d9d18a746c28 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Wed, 19 Jun 2019 17:43:37 +0100 Subject: [PATCH] perf(core): use `find` over `filter()[0]` This change brings a decent performance improvement when you are using `valueOf` with a lot of provided reusable elements --- packages/api-elements/CHANGELOG.md | 6 ++++++ packages/api-elements/lib/define-value-of.js | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/api-elements/CHANGELOG.md b/packages/api-elements/CHANGELOG.md index 56bbdfc2b..f1bf4f7b7 100644 --- a/packages/api-elements/CHANGELOG.md +++ b/packages/api-elements/CHANGELOG.md @@ -1,5 +1,11 @@ # API Elements (JavaScript) CHANGELOG +## Master + +### Enhancements + +- Performance of the `valueOf` has been improved under certain cases. + ## 0.2.0 (2019-06-11) ### Breaking diff --git a/packages/api-elements/lib/define-value-of.js b/packages/api-elements/lib/define-value-of.js index 0841780b0..ad7a06529 100644 --- a/packages/api-elements/lib/define-value-of.js +++ b/packages/api-elements/lib/define-value-of.js @@ -129,7 +129,7 @@ module.exports = (namespace) => { if (elements) { if (e.element === 'ref') { - const result = elements.filter(el => el.id.equals(e.content))[0]; + const result = elements.find(el => el.id.equals(e.content)); const inheritedElements = elements.filter(el => !el.id.equals(e.content)); if (e.path && e.path.toValue() === 'content') { @@ -139,7 +139,7 @@ module.exports = (namespace) => { return mapValue(result, opts, f, inheritedElements); } - const result = elements.filter(el => el.id.equals(e.element))[0]; + const result = elements.find(el => el.id.equals(e.element)); if (result) { const inheritedElements = elements.filter(el => !el.id.equals(e.element)); return mapValue(result, opts, f, inheritedElements);