Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

缓存 Object.prototype.toString

// util.ts

const toString = Object.prototype.toString;

export function isDate(val: any): boolean {
    return toString.call(val) === '[object Date]';
}

util.ts 的一个代码片段

  • 请问这样缓存 toString 与一级一级访问 Object.prototype.toString 在性能上有什么区别吗?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

对古早时代的浏览器有区别,对现在的都是 V8 内核来说没区别。

但一来少敲几下键盘,二来可以防止因原型链方法在后面被覆写而造成 Bug。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...