isdigit
JavaScript performance comparison
Info
Check whether a single character is a decimal digit ("0" to "9").
Preparation code
<script>
Benchmark.prototype.setup = function() {
function test(callback)
{
var values = [4, 2, "x", " ", "."];
var digits = 0;
var i = null;
var length = values.length;
for (i = 0; i < length; i += 1)
{
var character = values[i];
if (callback(character) === true)
{
digits += 1;
}
}
return digits;
}
function isDigitRange(character)
{
return (character >= "0" && character <= "9");
}
function isDigitCodeRange(character)
{
var c = ("" + character).charCodeAt(0);
return ((c >= 48) && (c <= 57));
}
function isDigitIndex(character)
{
return ("0123456789".indexOf(character) >= 0);
}
function isDigitSwitch(character)
{
switch (character)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
return true;
default:
return false;
}
}
function testIsNan(character)
{
return (!isNaN(character));
}
function testIsNanParse(character)
{
return (!isNaN(parseInt(character, 10)));
}
function testIsFinite(character)
{
return (isFinite(character));
}
function testIsFiniteParse(character)
{
return (isFinite(parseInt(character, 10)));
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
range |
|
pending… |
code range |
|
pending… |
index |
|
pending… |
switch |
|
pending… |
testIsNan |
|
pending… |
testIsNanParse |
|
pending… |
testIsFinite |
|
pending… |
testIsFiniteParse |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Ariya
- Revision 2: published by XP1
0 comments