fake_radio (v2)

Revision 2 of this benchmark created by jeff on


Description

ff

Preparation HTML

<form>
                        <p><input type="radio" id="male" name="sex" value="male"><leabel for="male">Male</label></p>
                        <p><input type="radio" id="female" name="sex" value="female" checked><label for="female">Female</label></p>
                </form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

erf

Teardown


    re
  

Test runner

Ready to run.

Testing in
TestOps/sec
no-jquery
// async test
function FakeRadio(element) {
        this.element = element;
        this.name = element.getAttribute('name');
        this.fake = this.createFake();
        this.radio = this.getAllRadio(this.getClosestForm(element), this.name);

        var self = this,
                update = function() {
                        with (self) {
                                update();
                        }
                };

        element.style.display = 'none';

        if (element.checked)
                this.fake.setAttribute('class', 'fake-radio active');

        this.element.onchange = update;
        this.fake.onclick = update;

        return this;
}

FakeRadio.prototype = {
        createFake: function() {
                var element = this.element
                                        .parentNode
                                        .insertBefore(document.createElement('span'), this.element);
                        
                element.setAttribute('class', 'fake-radio')

                return element;
        },

        getClosestForm: function(element) {
                if (element.nodeName === "FORM")
                        return element;
                else
                        return this.getClosestForm(element.parentNode);
        },

        getAllRadio: function(form, name) {
                var elements = [],
                        input = form.getElementsByTagName('input');

                for (var i = 0; i < input.length; i+= 1) {
                        var type = input[i].getAttribute('type').toLowerCase(),
                                inputName = input[i].getAttribute('name');

                        if (type === 'radio' && inputName === name)
                                elements.push(input[i]);
                }

                return elements;
        },

        update: function() {
                for (var i = 0; i < this.radio.length ; i += 1) {
                        if (this.radio[i].isEqualNode(this.element))
                                continue;

                        this.radio[i].cheked = false;
                        this.radio[i].previousSibling.setAttribute('class', 'fake-radio');
                }

                this.element.checked = true;
                this.fake.setAttribute('class', 'fake-radio active');
        }
}

jQuery.fn.fakeRadio = function() {
        return jQuery(this).each(function() {
                new FakeRadio(this);
        });
};
 
ready
jquery
function FakeRadio(element) {

        var element = jQuery(element),
                name = element.attr('name'),
                fake = jQuery('<span class="fake-radio" />').insertAfter(element),
                radio = element.closest('form').find('[name="' + name + '"]:radio');

        function update() {
                radio.each(function() {
                        jQuery(this)
                                .prop('checked', false)
                                .next('.fake-radio')
                                .removeClass('active');
                });

                fake.addClass('active')
                        .prev(':radio')
                        .prop('checked', true);
        }

        element.on('change', update);
        fake.click(update);

        if (element.is(':checked'))
                element.prop('checked', true).next('.fake-radio').addClass('active');

        return element;
} 

jQuery.fn.fakeRadio = function() {
        return jQuery(this).each(function() {
                new FakeRadio(this);
        });
};
 
ready

Revisions

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

  • Revision 2: published by jeff on