underscore.js の _.isRegExp について
underscore.js においては正規表現オブジェクトかどうかの判断を以下のようにしてやっている。
var toString = Object.prototype.toString;
function isRegExp(obj) {
return toString.call(obj)=='[object RegExp]';
}
これは
obj instanceof RegExp;
でいい気がするんだけど、なんかこれだとだめなのかな。
後者の方が高速だけど。
ベンチマーク: http://jsdo.it/tokuhirom/wfiw
【蛇足: オレはこう思う】
cho45 のベンチマークべんり。
【追記】
multiple frames のときにハマるということらしい。
http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
Published: 2011-12-05(Mon) 23:15