Skip to content

perf(core): getType uses a cache for well known types. #12123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
perf(core): getType uses a cache for well known types.
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 committed Mar 6, 2023
commit f06f11072ca278c376cab2e24ab30dff79e63afd
19 changes: 18 additions & 1 deletion src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy