jQuery detection

Benchmark created by Thomas Genin on


Description

fastest way to return a jQuery object given a string or a jQuery object

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="test">
</div>
<script type="text/javascript">
  var $elem = $("#test");
  var str = "#test";

  function instance(e) {
    if (e instanceof jQuery) return e;

    return $(e);
  }

  function type(e) {
    if (typeof e == 'string') return $(e);
    return e;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Instance of
instance($elem);
instance(str);
ready
Typeof
type($elem);
type(str);
ready

Revisions

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

  • Revision 1: published by Thomas Genin on