jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/
<script>
function splitOperation (strSource)
{
var lines = strSource.split('\n');
var output = '';
for (var i=0;i<lines.length;++i)
{
output += lines[i]+'\n';
}
return output;
}
function calculate(strSource)
{
var intSourceLength = strSource.length;
var part = 0;
var part1NumberFound = false;
var hashSignFound = false;
var operationSequence = '';
var part1CharacterFound = '';
var operation = '';
var minusSignFound = false;
var dotFound = false;
var operandStarted = false;
var operand = '';
var parsingErrorIndex=0;
var parsingErrorText='';
var result;
var operandIndex = 0;
var calculationoutput = '';
for (var i=0;i<intSourceLength;++i)
{
parsingErrorIndex++;
if (part == 0)
{
// Allowed Characters in this part are Numbers, Hash Character and Minus
if ((strSource[i] >= '0' && strSource[i] <= '9') || strSource[i] == '#' || strSource[i] == '-')
{
if (strSource[i] >= '0' && strSource[i] <= '9')
{
if (!hashSignFound)
{
operationSequence+=strSource[i];
part1NumberFound = true;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Number Should Not Follow Hash Sign";
}
}
else if (strSource[i] == '#')
{
if (part1NumberFound)
{
if (!hashSignFound)
{
hashSignFound = true;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Hash Sign Duplication";
}
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Hash Sign Found Before Line Number";
}
}
else if (strSource[i] == '-')
{
if (hashSignFound)
{
part=1;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Minus Sign Should always Follow Hash Sign";
}
}
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Only numbers Minus or Hash Sign Allowed";
}
}
else if (part == 1)
{
// Allowed Characters in this part are Characters and Colon Character
if ((strSource[i] >= 'A' && strSource[i] <= 'z') || strSource[i] == ':')
{
if (strSource[i] >= 'A' && strSource[i] <= 'z')
{
operation+=strSource[i];
part1CharacterFound = true;
}
else if(strSource[i] == ':')
{
if (part1CharacterFound)
{
result=0;
if (operation != 'SUM' && operation != 'MIN' && operation != 'MAX' && operation != 'AVERAGE')
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Invalid Operation";
}
part=2;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] the Colon Sign Should Follow the Operation";
}
}
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Only Characters or Colon Sign Allowed";
}
}
else if (part == 2)
{
// Allowed Characters in this part are Numbers, Comma, Dot, Space, New Line, Carrage Return and Minus
if ((strSource[i] >= '0' && strSource[i] <= '9') || strSource[i] == ',' || strSource[i] == '.' || strSource[i] == ' ' || strSource[i] == '\n' || strSource[i] == '\r' || strSource[i] == '-')
{
if (strSource[i] == '.')
{
if (!dotFound)
{
operand += strSource[i];
dotFound = true;
operandStarted = true;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Dot Character Duplicated";
}
}
else if (strSource[i] == '-')
{
if (!minusSignFound)
{
if (!operandStarted)
{
operand += strSource[i];
minusSignFound = true;
operandStarted = true;
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Cannot put Minus Sign after or Inside the Operand";
}
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Minus Sign Duplicated";
}
}
else if (strSource[i] == ' ')
{
operand += strSource[i];
}
else if (strSource[i] >= '0' && strSource[i] <= '9')
{
operand += strSource[i];
operandStarted = true;
}
else if (strSource[i] == ',')
{
var operandValue = parseFloat(operand);
if (operation == 'SUM')
{
result += operandValue;
}
else if (operation == 'MIN')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue < result)
{
result = operandValue;
}
}
}
else if (operation == 'MAX')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue > result)
{
result = operandValue;
}
}
}
else if (operation == 'AVERAGE')
{
result += parseFloat(operand);
}
operandIndex++;
minusSignFound = false;
dotFound = false;
operandStarted = false;
operand = '';
}
else if (strSource[i] == '\n')
{
var operandValue = parseFloat(operand);
if (operation == 'SUM')
{
result += operandValue;
}
else if (operation == 'MIN')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue < result)
{
result = operandValue;
}
}
}
else if (operation == 'MAX')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue > result)
{
result = operandValue;
}
}
}
else if (operation == 'AVERAGE')
{
result += parseFloat(operand);
}
operandIndex++;
calculationoutput+=parsingErrorText == ''?operationSequence+"#:"+operation+"="+(operation == 'AVERAGE'?result/operandIndex:result)+'\n':parsingErrorText+'\n';
part = 0;
part1NumberFound = false;
hashSignFound = false;
operationSequence = '';
part1CharacterFound = '';
operation = '';
minusSignFound = false;
dotFound = false;
operandStarted = false;
operand = '';
parsingErrorIndex=0;
parsingErrorText='';
operandIndex = 0;
}
}
else
{
parsingErrorText = "[Character No. "+parsingErrorIndex+"] Only Numbers, Comma, Dot, Space, New Line, Carrage Return and Minus Sign Allowed";
}
}
}
var operandValue = parseFloat(operand);
if (operation == 'SUM')
{
result += operandValue;
}
else if (operation == 'MIN')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue < result)
{
result = operandValue;
}
}
}
else if (operation == 'MAX')
{
if (operandIndex == 0)
{
result = operandValue;
}
else
{
if (operandValue > result)
{
result = operandValue;
}
}
}
else if (operation == 'AVERAGE')
{
result += parseFloat(operand);
}
operandIndex++;
calculationoutput+=parsingErrorText == ''?operationSequence+"#:"+operation+"="+(operation == 'AVERAGE'?result/operandIndex:result):parsingErrorText;
return calculationoutput;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
NormalSplit |
| ready |
Calculate |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.