flyweight jquery select_presentation

25
Custom select element rage Or, how I learned to stop worrying and love the DOM Ray Brooks August 2011

Upload: ray-brooks

Post on 02-Jul-2015

1.068 views

Category:

Technology


2 download

DESCRIPTION

This plugin implements a flyweight design pattern for jQuery custom select controls. By only holding one visible control element in memory at one time, it aims to reduce the number of DOM accesses and event binding operations. This can result in significantly reduced page render times on larger forms. It borrows accessibility design patterns from the Filament Group's jQuery UI Selectmenu keyboard navigable ARIA plugin, currently included with JQuery-UI. It requires jQuery 1.4+ and jQuery UI 1.8.7+ Core to run.

TRANSCRIPT

Page 1: Flyweight jquery select_presentation

Custom select element rageOr, how I learned to stop worrying and love the DOM

Ray BrooksAugust 2011

Page 2: Flyweight jquery select_presentation

Custom select element rage

Page 3: Flyweight jquery select_presentation

INPUTS! Y U NO STYLE?

INPUT, BUTTON, TEXTAREA, SELECT

• Levels of CSS support for these vary• It’s fiddly• It’s boring• You achieve inconsistent results• Multipart file inputs are a unique form (sic) of EVIL• But worst of all…

Microsoft’s TRIDENT rendering engine (IE 6,7,8) uses O/S controls as opposed to browser objects and the two are different beasts. C++ doesn’t understand CSS.

WE CAN’T STYLE THEM AT ALL

Page 4: Flyweight jquery select_presentation

A wild solution appears!• Felix Nagel has forked & extended Filament’s jQuery UI• His solution requires jQuery & Javascript• It’s styleable• It’s accessible• It supports ARIA• Seems to work well even in IE6+!

Check out the repo: https://github.com/fnagel/jquery-ui/

*le jQuery UI SelectMenu

Page 5: Flyweight jquery select_presentation

Felix Nagel’s fork of jQuery UI, I choose you!

Page 6: Flyweight jquery select_presentation

LBi uses Very Big Form

Page 7: Flyweight jquery select_presentation

It’s not very effective!

Number of SELECT elements

Load time (m

s)

ONE SELECT ELEMENT TAKES ~800ms TO RENDER IN IE8

problem?

Page 8: Flyweight jquery select_presentation

OH NOES!• Complex elements such as SELECT have been simulated in full using much markup and many event bindings• The SELECT replacement code in particular makes many DOM accesses via jQuery, which is slow• It’s not such a big problem with browsers that can optimise Javascript, however…• This is a very big page, and• Not all browsers can optimise Javascript• OH, SWEET IRONY! They’re also the browsers we are most interested in supporting!

Page 9: Flyweight jquery select_presentation

The Challenge

Client’s page load time is 90s in IE8, 170s in IE7 + IE6!Reduce page load time to under 10 seconds for all level 1 and 2 browsers (Chrome 10+, FF 3+, IE7+, Safari 3+)

Page 10: Flyweight jquery select_presentation
Page 11: Flyweight jquery select_presentation

Flyweight pattern

“The intent of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.”

http://www.oodesign.com/flyweight-pattern.html

Page 12: Flyweight jquery select_presentation

Our solution• Delay as much as possible until after page load completes• Construct only one anchor to replace one SELECT element• Share one drop down menu between all SELECT elements• Construct markup as strings, which is fast

• Convert strings to objects as late as possible• Cache SELECT values in memory for fast retrieval• Bind events only to top level elements and use bubbling• Optimise all loops• For speed, use as much pure Javascript as possible

Page 13: Flyweight jquery select_presentation

Notwithstanding…• Behaviourally identical to SELECT• Accessible• Semantic• Graceful

And of course…

Page 14: Flyweight jquery select_presentation
Page 15: Flyweight jquery select_presentation

Right, this shouldn’t take me very long

Page 16: Flyweight jquery select_presentation
Page 17: Flyweight jquery select_presentation

Hm. I obviously need to get better at estimating this kind of shit

Page 18: Flyweight jquery select_presentation

LET’S SEE SOME REAL WORLDPERFORMANCE TESTING, THEN

Page 19: Flyweight jquery select_presentation

IE6 XPSP2 VM60 SELECTS312ms~5ms/SELECT

Page 20: Flyweight jquery select_presentation

IE8 XPSP2 VM60 SELECTS219ms<4ms/SELECT

Page 21: Flyweight jquery select_presentation

Chrome 13 native60 SELECTS97ms<2ms/SELECT

Page 22: Flyweight jquery select_presentation

HALLELUJAH

Page 23: Flyweight jquery select_presentation

flyweight-jquery-custom-select• Lightweight• Extremely fast• Semantic markup• Fully customisable keyboard navigation• Fully customisable class names• Intelligent typeahead • Option group support• Enable/disable• Graceful destroy, original select maintains state• Supports CSS3• Supports PIE.htc for IE6/7/8 CSS3 emulation• Toggle hide first option element (for Please Select, etc)• Binds to original select change event• Auto scroll• Only 8KB compressed• Works in all tested major browsers

• FF 3.6/5.1, Chrome 11/12, Safari 5, IE 6/7/8

Page 24: Flyweight jquery select_presentation

Examples of usage

default $("select").flyweightCustomSelect();

with options $("select").flyweightCustomSelect({

optionfilter:'option,optgroup',pleaseselect:true

});

Page 25: Flyweight jquery select_presentation

https://github.com/rayui/flyweight-jquery-custom-select

And that’s why we can now begin to use custom selects on very big formsThanks for listening!