3 ms·
Since those two things aren't really the same, I'd say this is a terrible example (a strawman even). If you want the clientWidth, use the clientWidth. jQuery
by jackmoore 13y ago
Since those two things aren't really the same, I'd say this is a terrible example (a strawman even). If you want the clientWidth, use the clientWidth. jQuery works in tandem with the native DOM, and is not meant to be a wholesale replacement:
$("div").sort(function(a, b) {
return a.clientWidth * a.clientHeight - (b.clientWidth * b.clientHeight);
});
The speed for this is slower, as expected, but comparable to native. http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6/4 http://jsperf.com/d97b341f-cfc1-4057-bdc9-60e80adb5cf6/4
- ivanca 13y agoIn that context any kind of comparision would be a strawman unless is _your_ way of mixing jQuery with native code; maybe this would become more clear with different examples: #Ex. 1 $("div").map(function(){ return $(this).css('background-image'); }) "If you just want the background-image just use the get-computed-style-background-image!" [].slice.call(documents.getElementsByTagName('div')).map(function(a){ return getComputedStyle(a)["background-image"]; }); #Ex. 2 $("div").sort(function(a, b){ return $(a).find('*').length - $(b).find('*').length; }); "If you just want an algorithm that sorts the ones with the biggest amount of children and grandchildren just use an algorithm that sorts the ones with the biggest amount of children using domElement.getElementsByTagName" [].slice.call(documents.getElementsByTagName('div')).sort(function(a){ a.getElementsByTagName('*').length - b.getElementsByTagName('*').length; }); >And is not meant to be a wholesale replacement I never said such thing neither; now that is 100% a straw-man.