Client-side full text search in CSS
redotheweb.com
redotheweb.com
But this:
>The advantage of using CSS selectors rather than JavaScript indexOf() for search is speed: you only change one element at each keystroke (the <style> tag) instead of changing all the elements matching the query.
makes it sound like they just don't know how to write DOM-efficient JS, and probably never profiled it or their implementation. I would be shocked if you can't relatively-trivially make a faster JS implementation, and even more if you can't make a significantly faster 'smarter' one with e.g. a more optimized search index, since you can make tradeoffs the CSS processor very likely cannot.
> I would be shocked if you can't relatively-trivially make a faster JS implementation
I'm not brave enough to do a jsperf testcase, but out of my frontend profiling experience, I would guess that his solution is significantly faster that the naive one you see in most of the case using jQuery, i.e.
$('.elements').each(function() {
$(this).toggle(matchSomeCriterias(this))
})
This naive solution is slow because: - jQuery.toggle() assign inline style on each elements.
- It directly fetch values from the DOM to match nodes
- It trigger a lot of reflows
But yes, I'm like you pretty sure that a decent implementation that work on an in memory mapping of <value to match> => <DOM node>, use class toggling to show/hide element and make use of document.createDocumentFragment() beat this CSS method.Poor javascript performance is most of the time due to excessive reflow triggering, this CSS beat naive JS solutions because it only trigger one.
Edit: oh and an issue I had at the time was that for IE8 and older you can't use element.innerHTML on a style element. You have to use element.styleSheet.cssText
[1]: http://cl.ly/image/2I1v3H053L0Y jQuery really is great, but getting away from it is great too.
Yeah you easily get bitten by jQuery because it mix reads with writes under the hood.
I prefer to use it most of the time for browser compat / easier maintainability, but it's crazy the performance you can save by bypassing it in hotspots.
For example, enter this into the search field:
"]), body, a:not([data-index="
This will hide the entire page. The last "a:not" selector is really inconsequential-- I just had to close the opening parenthesis and this just happens to work.(It may sound like I'm trying to be an ass, but I am actually curious).
You can also clickjack, i.e. make a button that does something important invisible and stretch it across the entire page. Next time the user clicks, they'll inadvertently be clicking the button.
Edit: I did some research and testing and it looks like XBL and element behaviors are no longer possible in Firefox and IE 10, thankfully:
http://stackoverflow.com/questions/9679527/do-moz-behaviors-... http://msdn.microsoft.com/en-us/library/ie/hh801219%28v=vs.8...
Another problem that would only start to show up on a larger dataset is that because the index is all concatenated directly together, it matches strings that span several words. A user searching for their pal Harry Mesbro in the list might be confused to find that typing in his last name also brings up Yvette Hammes.
The example only supports single words because it contains only single-word data. If you edit one of the indexes to include a space it works.
But no, it won't work correctly just by adding spaces to the index. That will only work if the fields you enter are adjacent. So if you typed "Ona Bednar", it would work, but "Bednar Ona" won't. That's not how users expect search to work.
For a more ordinary example of what I mean, suppose the dataset included the middle name. Users will expect to find Ona Justine Bednar if they type "Ona Bednar". If it doesn't work that way, it's broken.
.searchable { display: none; }
.searchable[data-index*="term1"][date-index*="term2"] { display; block; }
and an empty input would hav eno selectors (or .searchable {display:block;} ).It's slightly more code, but much more usable.
Got stuck on that for about 5 minutes. That's what I get for copy pasting :)
I chuckled after reading the article at the somewhat misleading title. I was assuming it was pure CSS, no JS...and was wondering how the CSS was firing events! :)
This is still pretty neat, IMO, and I like how it emphasizes the "markup as your data model" concept.
This will be used when the data is already sent to the client side.
The search is certainly useful with 100 records, but if I can return only the 10 matching records from an AJAX request, that's 90 records I didn't have to return in the first place.
Using multiple textboxes, apply each search term to a nested mongo resultset. Visually narrow down the data structure you want to get out of mongo, and generate the mongo query you'd use to get that data.
Plus, one would have to write that client side code to parse all that text to insert into said data attributes.
Although, I suppose if you're just doing this on a list of data, as in the example, then it would likely be reasonable.
Where's the performance comparison?