Condition VS RegExp VS indexOf
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var a = 1,
b = 8;
var gTab = [1, 2, 3, 4, 5];
var gRg = /^1|2|3|4|5$/;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Simple condition |
|
pending… |
indexOf |
|
pending… |
indexOf (not litteral) |
|
pending… |
RegExp |
|
pending… |
RegExp (not litteral) |
|
pending… |
RegExp (global) |
|
pending… |
indexOf (global) |
|
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 Nicolas Froidure
- Revision 2: published by Nicolas Froidure
- Revision 3: published by Nicolas Froidure
- Revision 4: published
3 comments
if (a == 1 || a == 2 || a == 3 || a == 4 || a == 5) {
if (b == 1 || b == 2 || b == 3 || b == 4 || b == 5) {
Nop?
Test modified and with comment on && :) http://jsperf.com/condition-vs-regexp-vs-every-vs-indexof/3
Consequences will never be the same :)