/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] i require a faster way to do this:

the following code is seemingly quick when only a few “pairs” are returned, but as the # of pairs grows, the slower this gets. any help finding a way to make this code faster, and/or more efficient is most appreciated.

[CODE]
function get_users_forDropdowns(eid) {
// where ‘eid’ is the ID of the select input to update
var e = document.getElementById(eid);
if (e) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
if (xhttp.responseText) {
//console_msg(“data received!”);
var a = xhttp.responseText.split(“—–“); // does splitting on so many characters slow the process?
e.innerHTML = “”;
for (i = 0; i < a.length; i++) {
var b = a[i].split(“=”);
e.innerHTML += “<option value=\”” + b[0] + “\”>” + b[1];
}
} else {
//console_msg(“no data received!”);
}
}
}

e.innerHTML = “<option>loading…”;
xhttp.open(“GET”, “getusers.pl?a=1&o=1”, true);
xhttp.send();
console_msg(“requesting user ID’s…”);
} else {
console_msg(“HTML element ID ‘” + eid + “‘ does not exist!”);
}
}
[/CODE]

jamroll

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@JamRollauthorAug 22.2017 — oh, [B]getusers.pl[/B] returns a string like this:

ID=name-----ID=name-----ID=name---...
Copy linkTweet thisAlerts:
@rootAug 22.2017 — Why not have the server return a JSON string and parse it in the browser and create an object that contains the data and then you don't need to split strings and filter them to access the content returned.
Copy linkTweet thisAlerts:
@JamRollauthorAug 22.2017 — Hi, Super Moderator!

I am not using JSON. I don't understand what you mean by "parsing in the browser" (the above code is client-side, if that's what yer meanin). Why don't i use JSON? Straight up, I'm not really interested in learning the "language" of JSON (because i already have enough to worry about without adding to my woes with yet another new way of doing things). Unless including JSON into this [B]one[/B] function is SUPER simple, and uses little to no resources (client and/or server side), then okay, i'll give it a go, otherwise i'd like to find a normal javascript way.
Copy linkTweet thisAlerts:
@SempervivumAug 23.2017 — When analyzing performance issues in a configuration like this, you need to look at the server side script too. Measure the response times first by the developer tools of your browser. Preparing the information on the server might include access to a database which possibly can be optimized.

Reading the client side script you posted I do not see any way how to optimize it.
Copy linkTweet thisAlerts:
@rootAug 23.2017 — Hi, Super Moderator!

I am not using JSON. I don't understand what you mean by "parsing in the browser" (the above code is client-side, if that's what yer meanin). Why don't i use JSON? Straight up, I'm not really interested in learning the "language" of JSON (because i already have enough to worry about without adding to my woes with yet another new way of doing things). Unless including JSON into this [B]one[/B] function is SUPER simple, and uses little to no resources (client and/or server side), then okay, i'll give it a go, otherwise i'd like to find a normal javascript way.[/QUOTE]


JavaScript Object Notation (JSON) is a data-interchange format that is human readable and when parsed with JSON.parse, the output is turned from a string directly in to a variable, for example, [B]myObject = JSON.parse( data-interchange-string )[/B] will turn the passed argument (a string ) in to an object.

Imagine that this is a JSON String...{
1:"cheese",
"mice":"elephants",
"mango":57
}
not much as it is, but parse it and then myObject.mice will = elephants, myObject[1] = cheese...

JSON

Your request can then be simpler, by requesting data via a PHP script on the server and using PHP's JSON functions, you could use a database to provide the routine you write to push out a JSON formatted string as the response, the clients browser then parses that data string and returns a working variable as an Object that you can easily access the data in that variable and requires no splitting or filtering or manipulation of strings in URL's which is never a good idea as passing data in URL's in any case.
Copy linkTweet thisAlerts:
@KeverAug 23.2017 — Every cycle of the loop you're adding to the innerHTML, which means the HTML you're adding and the current HTML of the selectbox has to be parsed every cycle. By using the DOM selectbox API you can make it run a lot faster.

<i>
</i>function get_users_forDropdowns(eid) {
var el = document.getElementById(eid),
xhttp;

if(el == null) {
console_msg("HTML element ID '" + eid + "' does not exist!");
} else {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = xhttp.responseText.split("-----");
for(i=0; i&lt;ret.length; i++) {
temp = ret[i].split("=");
el.add(new Option(temp[0], temp[1]);
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};

<i> </i>el.add(new Option("loading...");
<i> </i>xhttp.open("GET", "getusers.pl?a=1&amp;o=1", true);
<i> </i>xhttp.send();
<i> </i>console_msg("requesting user ID's...");
}
};
or with JSON response<i>
</i> xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = JSON.parse(xhttp.responseText); // JSON response '{"uservalue":"username", ...}'
for(i in ret) {
if(ret.hasOwnProperty(i)){
el.add(new Option(ret[i], i);
}
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};


What kind of device are you testing this on? Because I need to add over a thousand options to make it feel slow. You shouldn't want to present a hundreds of options selectbox to you're users.
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — When analyzing performance issues in a configuration like this, you need to look at the server side script too. Measure the response times first by the developer tools of your browser. Preparing the information on the server might include access to a database which possibly can be optimized.

Reading the client side script you posted I do not see any way how to optimize it.[/QUOTE]


if yer meanin [B]getusers.pl[/B], it returns the list in under 1 second - even if that list is 1000 entries long
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — JavaScript Object Notation (JSON) is a data-interchange format that is human readable and when parsed with JSON.parse, the output is turned from a string directly in to a variable, for example, [B]myObject = JSON.parse( data-interchange-string )[/B] will turn the passed argument (a string ) in to an object.

Imagine that this is a JSON String...{
1:"cheese",
"mice":"elephants",
"mango":57
}
not much as it is, but parse it and then myObject.mice will = elephants, myObject[1] = cheese...

JSON

Your request can then be simpler, by requesting data via a PHP script on the server and using PHP's JSON functions, you could use a database to provide the routine you write to push out a JSON formatted string as the response, the clients browser then parses that data string and returns a working variable as an Object that you can easily access the data in that variable and requires no splitting or filtering or manipulation of strings in URL's which is never a good idea as passing data in URL's in any case.[/QUOTE]


okay. that sounds really easy. except, i'm coding in perl, not PHP...don't mean to be difficult ?
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — Every cycle of the loop you're adding to the innerHTML, which means the HTML you're adding and the current HTML of the selectbox has to be parsed every cycle. By using the DOM selectbox API you can make it run a lot faster.

<i>
</i>function get_users_forDropdowns(eid) {
var el = document.getElementById(eid),
xhttp;

if(el == null) {
console_msg("HTML element ID '" + eid + "' does not exist!");
} else {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = xhttp.responseText.split("-----");
for(i=0; i&lt;ret.length; i++) {
temp = ret[i].split("=");
el.add(new Option(temp[0], temp[1]);
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};

<i> </i>el.add(new Option("loading...");
<i> </i>xhttp.open("GET", "getusers.pl?a=1&amp;o=1", true);
<i> </i>xhttp.send();
<i> </i>console_msg("requesting user ID's...");
}
};
or with JSON response<i>
</i> xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = JSON.parse(xhttp.responseText); // JSON response '{"uservalue":"username", ...}'
for(i in ret) {
if(ret.hasOwnProperty(i)){
el.add(new Option(ret[i], i);
}
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};


What kind of device are you testing this on? Because I need to add over a thousand options to make it feel slow. You shouldn't want to present a hundreds of options selectbox to you're users.[/QUOTE]


i will give this a try and report back.
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — Every cycle of the loop you're adding to the innerHTML, which means the HTML you're adding and the current HTML of the selectbox has to be parsed every cycle. By using the DOM selectbox API you can make it run a lot faster.

<i>
</i>function get_users_forDropdowns(eid) {
var el = document.getElementById(eid),
xhttp;

if(el == null) {
console_msg("HTML element ID '" + eid + "' does not exist!");
} else {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = xhttp.responseText.split("-----");
for(i=0; i&lt;ret.length; i++) {
temp = ret[i].split("=");
el.add(new Option(temp[0], temp[1]);
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};

<i> </i>el.add(new Option("loading...");
<i> </i>xhttp.open("GET", "getusers.pl?a=1&amp;o=1", true);
<i> </i>xhttp.send();
<i> </i>console_msg("requesting user ID's...");
}
};
or with JSON response<i>
</i> xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = JSON.parse(xhttp.responseText); // JSON response '{"uservalue":"username", ...}'
for(i in ret) {
if(ret.hasOwnProperty(i)){
el.add(new Option(ret[i], i);
}
}
el.remove(0);
} else {
console_msg("no data received!");
}
}
};


What kind of device are you testing this on? Because I need to add over a thousand options to make it feel slow. You shouldn't want to present a hundreds of options selectbox to you're users.[/QUOTE]


okay. that sounds really easy. except, i'm coding in perl, not PHP...don't mean to be difficult ?[/QUOTE]

a BAZILLION times faster! wow, thank you, Kever!

here's what I did:

<i>
</i>/////////////////////////////////////////////////////////////////////
function get_users_forDropdowns2(eid) {
var el = document.getElementById(eid),
xhttp;

if(el == null) {
//console_msg("HTML element ID '" + eid + "' does not exist!");
} else {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var ret,
temp,
i;
if(xhttp.readyState == 4 &amp;&amp; xhttp.status == 200) {
if(xhttp.responseText) {
ret = xhttp.responseText.split("-----");
for(i=0; i&lt;ret.length; i++) {
temp = ret[i].split("=");
el.add(new Option(temp[0], temp[1]));
}
el.remove(0);
} else {
//console_msg("no data received!");
}
}
};

<i> </i>el.add(new Option("loading..."));
<i> </i>xhttp.open("GET", "getusers.pl?a=1&amp;o=1", true);
<i> </i>xhttp.send();
<i> </i>//console_msg("requesting user ID's...");
}
}


i barely get to see the "loading..." option! MOST excellent. glad to see i was right about my javascript code being the bit that was slow. this is a giant leap forward for this chunk of code. i will cite your name (for this forum) on my website's about page, with permission of course. may i? i will link to your profile here, if that's cool, too.

i knew there was a way to do this without JSON. Perfectly done. Again, many thanks.

Sincerely,

JamRoll
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — thank you all for your response. every last one was constructive and insightful. most appreciated.

sincerely grateful,

jamroll
Copy linkTweet thisAlerts:
@rootAug 23.2017 — You should note that JSON strings can be written as plain text file, I mentioned PHP because it offers functions to make making JSON object strings easier where data comes from a database.

... and what kind of server are you on? PHP is like the defacto standard on any hosting service (along with other serversides and server scripting services), if not that then get a host that does give you server-sides.
Copy linkTweet thisAlerts:
@JamRollauthorAug 23.2017 — You should note that JSON strings can be written as plain text file, I mentioned PHP because it offers functions to make making JSON object strings easier where data comes from a database.

... and what kind of server are you on? PHP is like the defacto standard on any hosting service (along with other serversides and server scripting services), if not that then get a host that does give you server-sides.[/QUOTE]


i am the host. even have a web address (.ca, no less - fer a buck) Ubuntu LAMP server. So, PHP is there, i just don't use it. I code in the following languages/scripts, and conventions:

PERL

JavaScript and AJAX

HTML

CSS

and SQL (sqlite database)

being that I am CEO, developer, and host, i'd like to adhere to the KISS concept as close as possible, and so i avoid having to incorporate what amounts to over-killing my code. inserting JSON may make some jobs easier, sure. but, you gotta learn it first. that's what i'd like to avoid. it's gotta be simple, robust solutions.
Copy linkTweet thisAlerts:
@KeverAug 23.2017 — Glad it work fine now. You may cite my nickname. No need to link to my profile though, there's not much on there since javascript is just a hobby for me. If you know javascript, then learning JSON should only take a couple of minutes since it looks quite like js objects. It's not a language but a data format. You only need to learn some conventions like quoting properties. You should check the link . posted earlier. It's only a 5 minute read.
Copy linkTweet thisAlerts:
@JMRKERAug 23.2017 — JSON made a lot me sense to me once I found and modified this bit of code. ?
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;
&lt;title&gt; JSON .stringify() / .parse() test &lt;/title&gt;

&lt;body&gt;
&lt;h1&gt;JSON.stringify() / .parse() test&lt;/h1&gt;
&lt;h2&gt;Create JSON string from a JavaScript Array and Object.&lt;/h2&gt;
&lt;hr&gt;
&lt;pre id="demo"&gt;&lt;/pre&gt;

&lt;script&gt;
// JSON array
document.getElementById("demo").innerHTML = '&lt;h2&gt;JSON Array&lt;/h2&gt;';
var arr = ["Lincoln", "alice", "beth", "Larson"];

var pick = JSON.stringify(arr); // convert array to string
document.getElementById('demo').innerHTML += '&lt;h3&gt;Storage&lt;/h3&gt;String arraynSome: '+pick+'n';

var arr2 = JSON.parse(pick); // convert string to a different array
var str = '&lt;h3&gt;Access:&lt;/h3&gt;Str =&gt; Arrayn'+arr2.join('n');
str += 'nnLast:'+arr2[arr2.length-1];
document.getElementById('demo').innerHTML += str+'&lt;hr&gt;';



document.getElementById("demo").innerHTML += '&lt;h2&gt;JSON Object&lt;/h2&gt;';
var obj = { "name":"John", "age":30, "city":"New York"};

var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML += '&lt;h3&gt;Storage&lt;/h3&gt;'+myJSON;

var arrObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML += '&lt;h3&gt;Access:&lt;/h3&gt;Name: '+arrObj.name
+ '&lt;br&gt; Age: '+arrObj.age
+ '&lt;br&gt;City: '+arrObj.city+'&lt;hr&gt;';
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;

Study it a few minutes and see how easy it is to go from string to object and back.
Copy linkTweet thisAlerts:
@HarryBSAug 25.2017 — A REST API defines a set of functions which developers can perform requests and receive responses via HTTP protocol such as GET and POST.

Because REST API’s use HTTP, they can be used by practically any programming language and easy to test (it’s a requirement of a REST API that the client and server are independent of each other allowing either to be coded in any language and improved upon supporting longevity and evolution).
×

Success!

Help @JamRoll spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...