判斷某些字符是否存在于一個字符串內(nèi),據(jù)我所知有兩種方法:正則的test方法/str/.test('testedString')字符串的indexOf方法'testString'.indexOf(str)那么具體就性能來說,哪種方法執(zhí)行速度更快一些呢?另外,是否有通用的性能測試的站點之類的?哈哈 樓下說得沒錯,確實應(yīng)該把時間花在更有意義的事情上另外測試結(jié)果已出,indexOf比reg.test快2倍多(function(){ var now = new Date()-1
var i = 10E6; while(i--){
/my/.test('mystring'); //~1400ms
'myString'.indexOf('my');//~600ms
} console.log(new Date() - 1 - now);
})()
判斷某個字符串是否存在,是reg.test快還是str.indexOf快?
慕尼黑的夜晚無繁華
2018-08-10 10:09:44