split vs for loop 2
Date tested:
2 years ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36
Test name
Executions per second
split
1835794.0 Ops/sec
for loop
7551.7 Ops/sec
Benchmark definition (click to collapse):
HTML Preparation code:
<script> var s="Since all three methods have roughly equivalent performance, my preference is to use slice. It supports extracting from the end of the string and I feel that returning an empty string when start index > stop follows the principle of least surprise better than substring's swapping of parameters. I avoid substr because of the browser inconsistency."; var d=" "; </script>
Tests:
split
var a=s.split(d); var i,l=a.length,w; for(i=0;i<l;i++){ w=a[i]; };
for loop
var i,l=s.length,w,lsp=-1; for(i=0;i<l;i++){ if(s.charAt(i) == d){ w=s.slice(lsp+1,i); lsp=i } };
Open this result on MeasureThat.net