darkening background-color of several divs by class name

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
.a { width: 10px; height: 10px; display: block; background-color: #123456 }
</style>
<div class="a"></div>
<div class="a"></div>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
with pure jquery
var jqElements = $('.a');
var newBgColor = jqElements.eq(0).css('background-color').match(/\d+/g).map(
        function(intValue) {
          return ((~~intValue) >> 1).toString(16);
        }).join("");
jqElements.css('background-color', "#" + newBgColor);
pending…
with mixed jquery and .style
var jqElements = $('.a');
var newBgColor = jqElements.eq(0).css('background-color').match(/\d+/g).map(
        function(intValue) {
          return ((~~intValue) >> 1).toString(16);
        }).join("");

        jqElements.each(function() {
            this.style.backgroundColor = "#" + newBgColor;
        })
pending…
knowing there's 2 elements
var jqElements = $('.a');
var newBgColor = jqElements.eq(0).css('background-color').match(/\d+/g).map(
        function(intValue) {
          return ((~~intValue) >> 1).toString(16);
        }).join("");

        jqElements.get(0).style.backgroundColor = "#" + newBgColor;
        jqElements.get(1).style.backgroundColor = "#" + newBgColor;
pending…
without jquery for background color
var jqElements = $('.a');
var newBgColor = window.getComputedStyle(jqElements.get(0)).backgroundColor.match(/\d+/g).map(
        function(intValue) {
          return ((~~intValue) >> 1).toString(16);
        }).join("");

        jqElements.each(function() {
            this.style.backgroundColor = "#" + newBgColor;
        })
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment