split vs for loop 2
Date tested:
5 years ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
Test name
Executions per second
split
219989.8 Ops/sec
for loop
81876.6 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