Re:Sprint – A high-performance, DOM library for modern browsers
by jackmoore on Wednesday April 22, 2015 @12:55PM
(#9421754)
DOM inconsistencies aside, a big advantage jQuery has over native API is that it is just as easy to work with multiple elements as it is single elements.
Modifying your example:
var d = document.querySelectorAll('div');
d = Array.prototype.slice.call(d);
d.forEach(function(el){ el.classList.add('new') });
Vesus: $('div').addClass('new');
Re:Sprint – A high-performance, DOM library for modern browsers
by rcgs on Wednesday April 22, 2015 @01:17PM
(#9421921)
It is possible to do that without the variable assignment in JS...
[].forEach.call(document.querySelectorAll('div'), function(el) {
el.classList.add('new');
});
But I don't disagree that the jQuery example is easier to write/read/remember.Though with frameworks like Angular or React I'm beginning to question whether we should ever be writing code like this at all.
Re:Sprint – A high-performance, DOM library for modern browsers
by jackmoore on Wednesday April 22, 2015 @04:03PM
(#9423167)
Agreed. I mainly use React these days, and directly modifying the DOM is starting to feel dirty.
Re:Sprint – A high-performance, DOM library for modern browsers
by BinaryIdiot on Wednesday April 22, 2015 @12:32PM
(#9421590)
There is a lot of uses of innerHTML in this code which causes browser reflow; might get better performance creating all of the HTML components via document.createElement() and using document.createTextNode() for all text within a tag itself.
But yeah cherry-picking may not be the most constructive thing however you do have good points. The DOM API is kinda funky (I wish it did some things like chaining) but it's very simple. Half the times I write a very thin wrapper around it JUST to provide some chaining.
Re:Sprint – A high-performance, DOM library for modern browsers
by sanderjd on Wednesday April 22, 2015 @02:11PM
(#9422327)
See, that's why I end up using these libraries. If I found myself doing the three statements for creating a div in your parent's comment, I'd pull that out into a function that does it, then I'd start finding other things to pull out as well, then I'd think "surely somebody has made a library that has these things pulled out nicely", and I'd find one of these libraries. Since I know that would happen, I just start with a library from the beginning.
But I suppose I'd love one that is thinner, like your "very thin wrapper", if anybody has pointers.
Re:Sprint – A high-performance, DOM library for modern browsers
by rwaldron on Wednesday April 22, 2015 @01:59PM
(#9422233)
You're conflating "JavaScript the programming language" with "DOM, the awful sack of shit".
Don't do that.
Re:Sprint – A high-performance, DOM library for modern browsers
by benjamindc on Thursday April 23, 2015 @04:55AM
(#9425546)
I find it interesting that, because some design decisions seem odd to you at first sight, you immediately conclude that the reason is a poor understanding of JavaScript. The `toArray` function, for example, is just performance optimization:
http://jsperf.com/toarray-slice-call-vs-loop
Re:Sprint – A high-performance, DOM library for modern browsers
by nicksardo on Wednesday April 22, 2015 @03:49PM
(#9423077)
Regarding toArray, the creator probably went that route to be more performant.
http://jsperf.com/array-prototype-slice-call-vs-slice-call/1...
Re:Sprint – A high-performance, DOM library for modern browsers
by jackmoore on Wednesday April 22, 2015 @12:40PM
(#9421649)
> I should fork the library and annotate every method with its vanilla Javascript equivalent
To save you some work: http://youmightnotneedjquery.com/
function toArray(o) { return Array.prototype.slice.call(o) }
Instead it's:
This is just silly: [...] Perhaps this isn't so constructive and I should fork the library and annotate every method with its vanilla Javascript equivalent. I'm sure this post is non-constructive in some respects but I like Javascript, I just wish people would learn how to work with the language it instead of replacing the API wholesale.