Usability aside: What is the fastest way to create a select
tag with 5'000 option
elements in IE6 from JavaScript?
Dr.Molle
Short test:
- Collecting a innerHTML-string first and inject it to the document: around 300ms
- appending options to an existing select-element using new Option(): around 25 sec
KooiInc
Appending options via DOM methods will cause a reflow/repaint of the screen for every option, slowing it all down. Using innerHTML after building a string (in memory) for your select is much faster (as Dr Molle told). An alternative to both methods would be to create an in memory documentFragment
, build your select object in it and finally append it's contents into the exististing DOM.
This aside from the question how a user should handle 5000 options of course (alas, the user isn't programmable ;)
来源:https://stackoverflow.com/questions/4538093/what-is-the-fastest-way-to-create-a-select-tag-with-5000-options-in-ie6