473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Large Array Iteration (200000) - any chance for optimization?

Hello!

My project has the following interface: (screenshot works the best)
http://www.screencast.com/users/asch...9-4a7af883e3c9

Device groups could have 1 device, or 100,000 devices.

I have an asynchronous action when you select a different device group
- it will go to the server, get the applicable devices - remove the
current devices from the list, then re-add all the devices it has
retrieved.

This takes a long time with around 20,000 devices (20-30 seconds).
Here's my code:

//result is an array of strings
function populateDeviceList(result){
var listBox = $('.DeviceList')[0];

//Clear the list
for (var i = listBox.options.length - 1 ; i >= 0 ; i--) {
listBox.options[i] = null;
}

//Repopulate with results
var deviceListFilterBox = $('.DeviceListFilterBox')[0].value;
var len = result.length;
for (var i = 0;i < len;i++){
var thisOne = result[i];
if (thisOne.startsWith(deviceListFilterBox)){
AddItem(listBox, thisOne, thisOne);
}
}
disableSelectedItems($('body'));
}

function AddItem(objListBox, strText, strId, added)
{
var newOption = new Option(strText, strId)
objListBox.options.add(newOption);
}

Any suggestions?

Thanks in advance!
Feb 19 '08 #1
10 2627
On Feb 19, 10:15 am, Adam Schaible <adam.schai...@gmail.comwrote:
Hello!

My project has the following interface: (screenshot works the best)http://www.screencast.com/users/asch...media/65d7db9d...

Device groups could have 1 device, or 100,000 devices.

I have an asynchronous action when you select a different device group
- it will go to the server, get the applicable devices - remove the
current devices from the list, then re-add all the devices it has
retrieved.

This takes a long time with around 20,000 devices (20-30 seconds).
Here's my code:

//result is an array of strings
function populateDeviceList(result){
var listBox = $('.DeviceList')[0];
In which library is $()? It is likely true that you can improve the
efficiency here by doing this DOM search manually. If you don't want
to do this manually give the $() some more help. Using a tagName or id
usually helps the most $('ul.DeviceList') or $('#firstDeviceList').
//Clear the list
for (var i = listBox.options.length - 1 ; i >= 0 ; i--) {
What are listBox.options? Is this jQuery and does the jQuery object
have an options array-like property?

listBox.options[i] = null;
}

//Repopulate with results
var deviceListFilterBox = $('.DeviceListFilterBox')[0].value;
var len = result.length;
for (var i = 0;i < len;i++){
var thisOne = result[i];
if (thisOne.startsWith(deviceListFilterBox)){
I don't know what startsWith is or how efficient it is.
AddItem(listBox, thisOne, thisOne);
It is odd to have a function start with a captial letter unless it is
preceded by "new"
}
}
disableSelectedItems($('body'));
}

function AddItem(objListBox, strText, strId, added)
{
var newOption = new Option(strText, strId)
objListBox.options.add(newOption);

}
It is difficult to make suggestions when there is clearly a library
involved and I don't know which one.

Try commenting out the body of the long for-loop and seeing how long
it takes. Then start adding in bits until you find the bit that makes
it take a long time. When you find that bit, optimize it.

Peter
Feb 19 '08 #2
Here's my code:
* * //Clear the list
* * for (var i = listBox.options.length - 1 ; i >= 0 ; i--) {
* * * * listBox.options[i] = null;
* * }
Why clear the list? Why not just do listBox.options=[];

When I do that, I get a javascript exception: setting a property that
has only a getter.
I did look at simply setting options to it's default state - but so
far the best method I've found is to iterate backwards through the
list.

Thanks for the suggestions so far!

Feb 19 '08 #3
Adam Schaible <ad***********@gmail.comwrites:
On a side note, does anyone have experience with arrays of this size?
If it's simply not possible, I'd rather move on!
Well, it seems you're adding lots and lots of data to some kind of
option list (/is/ it a <selector just something that vaguely looks
like it?)

Chances are high that the visitor won't be able to view more than about
a 100 items on a single screen. You may be able to take advantage of
that by just rendering the actual viewable data when it scrolls into
view (though it could get ugly & hard if you're using a plain <select>
box), and possibly filling in neighbouring pages in the background.

The big disadvantage is that your users probably won't be able to print
or use CTRL+F or any other built-in search mechanism for items that
aren't rendered yet.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Feb 19 '08 #4
Joost,

Thanks for the reply!

It is a plain old <selectand I'm adding Options to it.

Around 100 seems to be my convenience limit.

I'm moving towards rendering the entire select element server side and
simply replacing my current one with the new one asynchronously.

Thoughts?
Feb 19 '08 #5
Adam Schaible <ad***********@gmail.comwrites:
Joost,

Thanks for the reply!

It is a plain old <selectand I'm adding Options to it.

Around 100 seems to be my convenience limit.

I'm moving towards rendering the entire select element server side and
simply replacing my current one with the new one asynchronously.
You mean, using innerHTML? That should be relatively quick (the
insertion part), but it *will* become slow at some point. I think at
20,000 items you may already be approaching the limits of what's
acceptible. Also, transferring all that HTML around may be slow.
Thoughts?
You have a search box above the select, right? You could require a
minimal substring - say 3 characters - before you show anything. That
could bring down the number matches a lot.

Alternatively, just show the first 1000 or so matches (limit the output
at the server side), and show a message that there are more matches but
the user has to either "page" to the next 1000 items or refine the
search. Personally, I'd think this is a good mix of not trying to be too
clever and still giving the user the option of walking & searching
trough all items by hand if (s)he feels like it.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Feb 19 '08 #6
Ed:

That's a great suggestion - I got it going by doing it all "offline"
but it still took way too long. I think if I run it through the
server, can get a new select element in about 1 second, so I'll do it
through a web service.
Feb 19 '08 #7
On Feb 19, 11:34 am, Adam Schaible <adam.schai...@gmail.comwrote:
Here's my code:
//Clear the list
for (var i = listBox.options.length - 1 ; i >= 0 ; i--) {
listBox.options[i] = null;
}

Why clear the list? Why not just do listBox.options=[];

When I do that, I get a javascript exception: setting a property that
has only a getter.
I did look at simply setting options to it's default state - but so
far the best method I've found is to iterate backwards through the
list.

Thanks for the suggestions so far!
Then how about...
listBox.options.length=0;
Feb 19 '08 #8
On Feb 19, 3:11*pm, Adam Schaible <adam.schai...@gmail.comwrote:
Ed:

That's a great suggestion - I got it going by doing it all "offline"
but it still took way too long. *I think if I run it through the
server, can get a new select element in about 1 second, so I'll do it
through a web service.
Careful. IE fails to set the innerHTML of a select element properly.
You will need to feature test this and use outerHTML to replace the
entire select element when applicable.
Feb 19 '08 #9
Ok great, thanks for the tip!
Feb 19 '08 #10
On Feb 19, 1:15 pm, Adam Schaible <adam.schai...@gmail.comwrote:
Hello!

My project has the following interface: (screenshot works the best)http://www.screencast.com/users/asch...media/65d7db9d...

Device groups could have 1 device, or 100,000 devices.

I have an asynchronous action when you select a different device group
- it will go to the server, get the applicable devices - remove the
current devices from the list, then re-add all the devices it has
retrieved.
I don't know what library you are using, but look at the LiveGrid
example here : http://dowdybrown.com/dbprod/rico2/examples/php/ex2.php

This is not a native list control, and ajax calls would need to be re-
written, but the implementation should not be too difficult to write
for your need.
Feb 25 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
1
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the...
24
by: Frank Swarbrick | last post by:
We have a batch process that inserts large numbers (100,000 - 1,000,000) of records into a database each day. (DL/I database.) We're considering converting it to a DB2 table. Currently we have...
4
by: bcomeara | last post by:
I am writing a program which needs to include a large amount of data. Basically, the data are p values for different possible outcomes from trials with different number of observations (the p...
1
by: zaeminkr | last post by:
Hello. I believed below two functions would have same performance. However, I realized the second form of array iteration are found more in many performance critical code like Microsoft CRT...
3
by: katak | last post by:
hi... 1. how to do ranking value in array ? let said i hv 200000 sets of data need to store in an array after that i need to do ranking for the 200000 sets of data and find out the top 20 sets...
26
by: mike-yue | last post by:
The topic comes from a question: Would you rather wait for the results of a quicksort, a linear search, or a bubble sort on a 200000 element array? 1Quicksort 2Linear Search 3Bubble Sort ...
6
by: Terry Carroll | last post by:
I am trying to do something with a very large tarfile from within Python, and am running into memory constraints. The tarfile in question is a 4-gigabyte datafile from freedb.org,...
6
by: Patrick Sullivan | last post by:
Hello. I will be using some large data sets ("points" from 2 to 12 variables) and would like to use one class for each point rather than a list or dictionary. I imagine this is terribly...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.