jquery - What is faster/more efficient - $(".selector").height() vs $(".selector").eq(0).height() -
i have 20 x element.selector same (100px
) height , need height (100px
, not 2000px
).
what faster/more efficient do?
$(".selector").height()
or
$(".selector").eq(0).height()
i raced them you.
run 1 run 2 run 3 run 4 with_eq: 1956.769ms 1875.220ms 1930.814ms 1895.359ms no_eq: 1851.168ms 1861.596ms 1804.347ms 1829.207ms
and seems, not using eq(0)
faster. it's obvious, because save function call on jquery object. not calling faster call.
this test case:
// noprotect console.time('with_eq'); (var = 0; i<40000; i++) { $(".foo").eq(0).height(); } console.timeend('with_eq'); console.time('no_eq'); (var = 0; i<40000; i++) { $(".foo").height(); } console.timeend('no_eq');
.foo { height:100px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div> <div class="foo">hi.</div><div class="foo">hi.</div><div class="foo">hi.</div>
Comments
Post a Comment