Skip to content

Commit

Permalink
Optimize toPath method by caching results (final-form#329)
Browse files Browse the repository at this point in the history
* Optimize toPath performance by caching results

* Update bundle size
  • Loading branch information
jeslabs authored Mar 30, 2020
1 parent fb0a46c commit 359b7e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
},
{
"path": "dist/final-form.cjs.js",
"maxSize": "9.0kB"
"maxSize": "9.1kB"
}
],
"dependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/structure/toPath.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// @flow
const keysCache: {[string]: string[]} = {}
const keysRegex = /[.[\]]+/;

const toPath = (key: string): string[] => {
if (key === null || key === undefined || !key.length) {
return []
}
if (typeof key !== 'string') {
throw new Error('toPath() expects a string')
}
return key.split(/[.[\]]+/).filter(Boolean)
if(keysCache[key] == null) {
keysCache[key] = key.split(keysRegex).filter(Boolean)
}
return keysCache[key];
}

export default toPath

0 comments on commit 359b7e7

Please sign in to comment.