Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find n-th occurrence of a character
(version: 11)
Comparing performance of:
for vs while vs recur vs continuation vs throw-catch vs generator
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="for"></div> <div id="while"></div> <div id="rec"></div> <div id="cc"></div> <div id="catch"></div> <div id="gen"></div>
Script Preparation code:
function nthIndexOfFor(text, search, n) { let i = 0; n--; for (i = text.indexOf(search); n > 0; i = text.indexOf(search, i + 1), n--) { if (i === -1) return -1; } return i; } function nthIndexOfWhile(text, search, n) { let i = text.indexOf(search); while (--n > 0) { if (i === -1) return -1; i = text.indexOf(search, i + 1); } return i; } function nthIndexOfRec(text, search, n) { const recur = (n, i) => { if (n <= 0) return i; const j = text.indexOf(search, i + 1); if (j === -1) return -1; return recur(n - 1, j); } return recur(n, -1); } function nthIndexOfCc(text, search, n) { let i = -1; const ret = (v) => i = v; const recur = (n, i) => { if (n <= 0) return void ret(i); const j = text.indexOf(search, i + 1); if (j === -1) return void ret(-1); recur(n - 1, j); } recur(n, -1); return i; } function nthIndexOfCatch(text, search, n) { const recur = (n, i) => { if (n <= 0) throw i; const j = text.indexOf(search, i + 1); if (j === -1) throw -1; recur(n - 1, j); } try { recur(n, -1); } catch (i) { return i; } } function nthIndexOfGen(text, search, n) { const recur = function*(n, i) { if (n <= 0) yield i; const j = text.indexOf(search, i + 1); if (j === -1) yield - 1; yield* recur(n - 1, j); }; return recur(n, -1).next().value; }
Tests:
for
let e = document.getElementById('for'); e.innerHTML = nthIndexOfFor('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfFor('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
while
let e = document.getElementById('while'); e.innerHTML = nthIndexOfWhile('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfWhile('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
recur
let e = document.getElementById('rec'); e.innerHTML = nthIndexOfRec('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfRec('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
continuation
let e = document.getElementById('cc'); e.innerHTML = nthIndexOfCc('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfCc('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
throw-catch
let e = document.getElementById('catch'); e.innerHTML = nthIndexOfCatch('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfCatch('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
generator
let e = document.getElementById('gen'); e.innerHTML = nthIndexOfGen('/1/23/7/3/19/61/2/4/27/18/', '/', 7) + ' ' + nthIndexOfGen('/1/23/7/3/19/61/2/4/27/18/', '/', 17);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
for
while
recur
continuation
throw-catch
generator
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'm ready to answer your question. Please go ahead and ask!
Related benchmarks:
Count string occurrence
while vs recurse vs continuation
Count char occurrence in string
Find index in string with recursion vs findIndex
Comments
Confirm delete:
Do you really want to delete benchmark?