Skip to content

Commit

Permalink
perf(core): getType uses a cache for well known types.
Browse files Browse the repository at this point in the history
getType is called a lot, and it just run the same regexp over and over on the same base types.
A cache increase its own efficiency by more than 80% for basic types.
On a real world application with a lot of components,
getType was profiled for 8% of call duration before this patch,
and about 0.5% after.
The impact is more limited for smaller applications.
  • Loading branch information
Glandos authored Nov 18, 2024
1 parent 9e88707 commit a6b4590
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/core/util/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,28 @@ const functionTypeCheckRE = /^\s*function (\w+)/
* because a simple equality check will fail when running
* across different vms / iframes.
*/
function getType(fn) {
function getTypeName (fn) {
const match = fn && fn.toString().match(functionTypeCheckRE)
return match ? match[1] : ''
}

/**
* Build a cache for known types.
* We could build it dynamically, but since array are sometime requested,
* it fill up the cache for nothing.
* Type list is taken from https://vuejs.org/v2/guide/components-props.html#Type-Checks
*/
const TYPE_CACHE = new Map(
[String, Number, Boolean, Array, Object, Date, Function, Symbol, null, undefined].map(fn => [fn, getTypeName(fn)]))

function getType (fn) {
const cached = TYPE_CACHE.get(fn)
if (cached !== undefined) {
return cached
}
return getTypeName(fn)
}

function isSameType(a, b) {
return getType(a) === getType(b)
}
Expand Down

0 comments on commit a6b4590

Please sign in to comment.