Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Scores comp
(version: 0)
Comparing performance of:
REGEX STRING vs LOOP w keys lookup
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 2000; i--;) { var str = 'c'; if (i === 1300) { str = 'i'; } arr.push({ status: str, score: 80, score_type: 0, status_date: '09-05-2016' }); } var _op = RegExp(/[po]/g); var _i = RegExp(/[^i]/g); var _c = RegExp(/[^c]/g); var _f = RegExp(/[^f]/g); var _xn = RegExp(/cn|nc|fn|nf/g); function computeStatus(arr) { arr = arr.map(obj => obj.status); /* flatten array into scores only*/ arr = arr.join('') /* stringify */ .replace(_op, 'c') /* treat passed & optional as completes */ .replace(_xn, 'i') /* treat not started & (fail or passed) as incomplete */ var Cstatus = arr.replace(_i, '') || arr.replace(_f, '') || arr.replace(_c, '') || 'n'; return Cstatus.charAt(0); } function isComplete(str) { var comps = { 'p': true, 'c': true, 'o': true }; return comps[str] || false; //test the string if it is a completion grade status - returns boolean. } function computeStatus2(arr) { var stat, foundN, foundF, foundP, foundC; for (var i = arr.length; i--;) { //' Now loop through the recordset and calculate the status that the overall course should have var obj = arr[i]; var keys = Object.keys(obj); stat = obj[keys[0]]; if (!stat) { //consider null status as not started stat = 'n'; foundN = true; } else { if (stat !== "i") { //process all non i status - n,f,p,c,o foundC = isComplete(stat); //look for completions foundF = (stat === 'f'); //look for failures foundN = (stat === 'n'); //look for not started } else { //exit with status of incomplete as soon as one is found return stat; } } } if (foundN) { //'if we found not started lessons and nothing else then course status is "not started" stat = "n"; if (foundC || foundF) { stat = "i"; //'if a mix of not started lessons and completed or failed lessons status is incomplete } } else { if (foundF) { //'if no lessons not started and a single lesson is failed then the course status is "failed". stat = "f"; } else { if (foundC) { //'if we get here then no lessons have been found to have been incomplete, not started or failed therefore set the lesson to "complete" stat = "c"; } } } return stat; }
Tests:
REGEX STRING
computeStatus(arr)
LOOP w keys lookup
computeStatus2(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
REGEX STRING
LOOP w keys lookup
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!
Related benchmarks:
Test for
Test for
split vs substr
Search in array of strings
Searching in arrays
Comments
Confirm delete:
Do you really want to delete benchmark?