473,703 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which way will take more memory?

Hi:

I want to show a set of records in the database table on the clicnt browser.
I have two ways to do this (writen in JScript):

1.The first way is:

<%
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
Response.write( obj("firstname" ) + "<br>");
obj.movenext();
}
obj.close();
%>
2. The second way is:
<%
var arr=new Array();
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
arr[arr.length] = new String(obj("fir stname"));
obj.movenext();
}
obj.close();

.......
for(i=0;i<arr.l ength;i++)
{
document.write( arr[i] + "<br>");
}
%>
I think the second way is more flexible but it will take nuch more memory
than the first way, because the array "arr" will take more memory. Am I
correct? Do you think the second way will need more server resources? Do you
have better idea to improve the second way?

The reason that I tried the second way is that I wanted to make my server
side programs (most writen in JScript) be object-based (not OOP) programming
style. But I am doubt that the object-based programs will take much more
memory or server resources.

Thank you very much!

Qing

Jul 19 '05 #1
21 3915
I don't know about resources - though if you set the recordset to NOTHING as
soon as you're done with it that should free those resources up - but the
second method will be noticeably faster.

- Wm "Arrays made me a better person" Morris
--
William Morris
Product Development, Seritas LLC
"Rabbit63" <hu******@hotma il.com> wrote in message
news:e3******** ******@tk2msftn gp13.phx.gbl...
Hi:

I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript):

1.The first way is:

<%
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
Response.write( obj("firstname" ) + "<br>");
obj.movenext();
}
obj.close();
%>
2. The second way is:
<%
var arr=new Array();
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
arr[arr.length] = new String(obj("fir stname"));
obj.movenext();
}
obj.close();

......
for(i=0;i<arr.l ength;i++)
{
document.write( arr[i] + "<br>");
}
%>
I think the second way is more flexible but it will take nuch more memory
than the first way, because the array "arr" will take more memory. Am I
correct? Do you think the second way will need more server resources? Do you have better idea to improve the second way?

The reason that I tried the second way is that I wanted to make my server
side programs (most writen in JScript) be object-based (not OOP) programming style. But I am doubt that the object-based programs will take much more
memory or server resources.

Thank you very much!

Qing


Jul 19 '05 #2
This will out perform both methods.

var sql = "select firstname from table1";
var obj = new ActiveXObject(" ADODB.Recordset ");
obj.open(sql,co nn,adOpenForwar dOnly,adLockRea dOnly);
var arr = obj.GetRows;
obj.close();

-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #3
"WIlliam Morris" wrote:

I don't know about resources - though if you set the recordset
to NOTHING as soon as you're done with it that should free
those resources up - but the second method will be noticeably
faster.


Re-read his post. He's using JScript on the server, so there is no need to
set the recordset to NOTHING (nor a means to do so).

And I'm unconvinced the second will be faster. That may have been true with
VBScript/IIS4, but this is a completely different situation. As has been
noted in other threads, VBScript and JScript have different strengths and
weaknesses, and each does some tasks faster than the other.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #4
"Rabbit63" wrote:

I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript):

1.The first way is:

<%
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
Response.write( obj("firstname" ) + "<br>");
obj.movenext();
}
obj.close();
%>
2. The second way is:
<%
var arr=new Array();
var sql = "select firstname from table1";
var obj=new ActiveXObject(" ADODB.Recordset ");
obj.open(sql, conn);
while(!obj.EOF)
{
arr[arr.length] = new String(obj("fir stname"));
obj.movenext();
}
obj.close();

......
for(i=0;i<arr.l ength;i++)
{
document.write( arr[i] + "<br>");
}
%>
I think the second way is more flexible but it will take nuch more memory
than the first way, because the array "arr" will take more memory. Am I
correct? Do you think the second way will need more server resources? Do you have better idea to improve the second way?

The reason that I tried the second way is that I wanted to make my server
side programs (most writen in JScript) be object-based (not OOP) programming style. But I am doubt that the object-based programs will take much more
memory or server resources.


We have a pretty large number of JScript/ASP apps that use the following
methodology, and have never had performance issues, despite fairly heavy
usage. Here's the basic template, in which I implement your task:

<% var Page = new PageVariables() %>
<HTML> ... <%=Page.NameLis t%> ... </HTML>
<%
function PageVariables() {
var CN = Server.CreateOb ject("ADODB.Con nection"),
NameList = []
CN.Open( {connection string} )
var RS = CN.Execute( {sql string} )
while(!RS.EOF) {
NameList.push(R S.Fields("first name").Item)
RS.MoveNext()
}
this.NameList = NameList.join(" <BR>\r\n")
try { RS.Close() } catch(e) {}
try { CN.Close() } catch(e) {}
}
%>

This approach has several benefits. The first is that you don't put anything
into the Response Buffer until all of your server-side processing is
completed. This maximizes your flexibility, since, for example, you may
choose to Response.Redire ct if certain conditions are met.

Another benefit is that your HTML is abstracted from your server-side
scripting (meaning it's no longer interlaced), so you can hand the HTML
template portion to your graphic designer and let him put layout around your
content tokens (<%=Page.varnam e%>) in whatever order he likes. Even if you
do the whole thing yourself, the separation if HTML and processing into two
sections vastly improves readability.

Next benefit: A clean namespace. If you need to do a little interlacing, you
don't have to worry about duplication of variable names, since everything is
a property of the Page object, and all of its transient variables have
vanished. This will never present a namespace conflict:

<% for (var i=0; i<Page.OptionLi st.length; i++) { %>
<OPTION VALUE="<%=Page. OptionList[i].Value%>">
<%=Page.OptionL ist[i].Text%></OPTION>
<% } %>

There was another suggestion that you use RS.GetRows(), but I'm betting it
wouldn't help you. For one thing, it returns a 2D array, which JScript does
not understand, and which actually gets flattened into a single array with
twice the length and none of the structure. More importantly, it's virtually
useless if you are asking for more than one field, which is usually the
case. I assume your example is a simplification of your task, just as my
example is a great simplification* * of my solution.

At this point, I'd like to address your specific question regarding
resources. Does this technique require more memory at run-time? Yes, but
trivially. The transient variables (local to the function) have extremely
short lifespans, and by the time you hit the HTML layout, the only thing in
memory is the actual content you will be displaying.
=====
**I almost never interlace, for example. If I want to build a set of OPTIONs
for a SELECT element, I construct a string containing all of the options in
all of their detail. This, in turn, I generate from a single array by
calling a custom Array method defined in a site-wide include of prototype
extensions. The second example above simplifies to the following (1)
processing and (2) presentation lines:

1. this.OptionList = OptionArray.toO ptions(defaultV al)
2. <SELECT><%=Page .OptionList%></SELECT>

Another simplification -- I *rarely* use SQL strings, except to call stored
procedures. And I always use ADODB.Command for inserts, which is a topic for
another thread. Furthermore, it is rare for me to request a single field
from the DB, as I suspect is the case for most people. I can think of a
bunch more, but I'm getting tired and hungry, so I'm going to end it here
leave the rest as an exercise.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #5
"Dave Anderson" <GT**********@s pammotel.com> wrote in message
news:u3******** ******@TK2MSFTN GP11.phx.gbl...
"WIlliam Morris" wrote:

I don't know about resources - though if you set the recordset
to NOTHING as soon as you're done with it that should free
those resources up - but the second method will be noticeably
faster.
Re-read his post. He's using JScript on the server, so there is no

need to set the recordset to NOTHING (nor a means to do so).


There is both a need and a means for setting a recordset to Nothing.
Here is an article:
http://groups.google.com/groups?thre...%40tkmsftngp03

That being said, I imagine GetString would have been the fastest and
least resource intensive option in this case.

-Chris
Jul 19 '05 #6
Dave Anderson wrote:

There was another suggestion that you use RS.GetRows(), but I'm
betting it wouldn't help you. For one thing, it returns a 2D array,
which JScript does not understand, and which actually gets flattened
into a single array with twice the length and none of the structure.
More importantly, it's virtually useless if you are asking for more
than one field, which is usually the case.

Actually, GetRows returns a VBArray
(http://msdn.microsoft.com/library/en...objVBArray.asp)
which jscript handles quite nicely
(http://www.aspalliance.com/PeterJohnson/JSGetRows.asp) .

I've never tested the performance of this, but I suspect that even though it
is heavier than a vbs array, it will still outperform a heavy cursor.

I will agree that there is probably less benefit in jscript than there is in
vbs.

Bob Barrows

Jul 19 '05 #7
"Chris Hohmann" wrote:

Re-read his post. He's using JScript on the server, so there
is no need to set the recordset to NOTHING (nor a means to do
so).
There is both a need and a means for setting a recordset to
Nothing.


Assuming that there is...
http://groups.google.com/groups?thre...%40tkmsftngp03
....this thread does not make the "need" argument. I'll grant that you may be
able to construct a need to set the .ActiveConnecti on property to Nothing,
but this discussion does not address setting the Recordset to Nothing.
That being said, I imagine GetString would have been the fastest
and least resource intensive option in this case.


Assuming a single field is requested, probably. But that is probably not the
case (I know his example only asks for one field, but it's an EXAMPLE, and
probably not fully representative of his real goal).
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #8
"Dave Anderson" <GT**********@s pammotel.com> wrote in message
news:O8******** ******@TK2MSFTN GP09.phx.gbl...
"Chris Hohmann" wrote:

Re-read his post. He's using JScript on the server, so there
is no need to set the recordset to NOTHING (nor a means to do
so).
There is both a need and a means for setting a recordset to
Nothing.


Assuming that there is...


Assuming there is what, a need or a means? I think we can both agree
that there is indeed a means to setting a recordset object to Nothing in
JScript. In fact there are several of them. As for the need, see below.
http://groups.google.com/groups?thre...%40tkmsftngp03
...this thread does not make the "need" argument. I'll grant that you may be able to construct a need to set the .ActiveConnecti on property to Nothing, but this discussion does not address setting the Recordset to Nothing.
You are correct, the aforementioned thread does not make a specific
argument for the need to set recordset objects to Nothing. That
agrument/assumption is mine, just as the argument/assumption that there
is no need to set recordset objects to Nothing in JScript is yours. My
rationale for the need to explicitly set recordsets to Nothing is this.
By explicitly setting a recordset object to Nothing, the resources used
by that object are released and as such can be used in the processing of
the remainder of the page. As such, this can improve the performance of
a page and reduce its overall resource requirements which was the
question posed by the OP.

While the above thread does not argue for the need to set recordsets to
Nothing, it does address the issue, although that is not readily
apparent on first read. Here's a snippet from message three (3) in the
thread:

--- begin ---
3) My solution: simply get a reference to the default value for an
unopened
recordset, which just happens to be Nothing (VT_DISPATCH):

//---- Custom 'Nothing' equivalent for disconnected recordsets ----
var oRS = Server.CreateOb ject('ADODB.Rec ordset');
var Nothing = oRS.ActiveConne ction;
var oRS = Nothing;
--- end ---

Take note of the last line in which the recordset object is set to
Nothing.

That being said, I imagine GetString would have been the fastest
and least resource intensive option in this case.


Assuming a single field is requested, probably. But that is probably

not the case (I know his example only asks for one field, but it's an EXAMPLE, and probably not fully representative of his real goal).


Even with multiple fields, GetString is the fastest method. GetString's
limitation is not the number of fields in the recordset, it is the
inability/difficulty in performing row, column or cell based processing.
Here's an article that analyses the pros and cons of various methods of
rendering data from a recordset.

http://aspfaq.com/2467

I would be genuinely interested in hearing about any method that could
out perform GetString.

-Chris
Jul 19 '05 #9
"Chris Hohmann" wrote:

While the above thread does not argue for the need to set
recordsets to Nothing, it does address the issue, although
that is not readily apparent on first read. Here's a snippet
from message three (3) in the thread:

var oRS = Server.CreateOb ject('ADODB.Rec ordset');
var Nothing = oRS.ActiveConne ction;
var oRS = Nothing;
This is superficial at best. The original post asked about setting the
..ActiveConnect ion property (detailing a specific error), and this response,
while describing a means to assign the value Nothing, redeclares* the
variable and assigns it the constant value Nothing. Why is it different from
this?

var oRS = Server.CreateOb ject('ADODB.Rec ordset')
oRS = Math.PI

From a practical standpoint, it isn't. You assign a constant value to a
variable -- not exactly earth-shattering stuff, and NOT AT ALL equivalent to
the vbscript:

Set oRS = Nothing

The VBScript keyword Nothing has a specific purpose -- to free resources. It
is absent from JScript for a reason. I have yet to see an argument more
sophisticated than "that's the way it's done in VBScript" for freeing memory
in garbage-collected JScript**. Perhaps you could provide one.
...Even with multiple fields, GetString is the fastest method.
GetString's limitation is not the number of fields in the
recordset, it is the inability/difficulty in performing row,
column or cell based processing...


That was more or less my point. I am aware that GetString() uses row AND
column delimiters, but simple tabular display is frequently insufficient,
and thus is GetString(). I beg forgiveness for my lack of clarity on this
point.
*Or worse -- erroneously assumes the var statement is equivalent to
VBScript's Set statement.

**Though you may have noted from my other post that I *do* close recordsets
and connections. At some point in the past, I read that when one of them is
destroyed, the close() method is implicitly called. Not sure how that
translates in the JScript world, I have always just closed them. But my
other point remains -- we NEVER set JScript variables to Nothing, and have
never suffered a resulting or performance/resource drain, as we did before
conversion from VBScript.
--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #10

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

Similar topics

17
6144
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
19
3076
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at runtime using malloc.In this last member of the structer "int last" is not
34
3433
by: sushant | last post by:
hi all, suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++) { for(j=0;j<=10;j++) { some code; }
27
5675
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most 1K bytes of data. 3) These structs are to be used by all of the other 500K lines in various places.
4
7623
by: **Developer** | last post by:
If I need to allocate memory, maybe because I'm going to: Marshal.StructureToPtr Does it matter which of the following I use? Marshal.AllocCoTaskMem Marshal.AllocHGlobal I know the arguments are different which would make one more convient in a given situation, but other that that does it make any signifficant difference.
5
3402
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character text field. I want to know which Data type I need to use so that it doesnt waste memory. thanks in advance, rAinDeEr
5
4163
by: collection60 | last post by:
Hi people, I am writing a library that will implement a binary file format. Well... I'm thinking, as a design, it would be nice to have my library able to promise to not use more than a certain amount of RAM. I could have a "SetCacheSize(long NewSize);" function. So as part of that promise, when required memory exceeds that limit, the extra data is saved to disk and flushed from RAM.
7
20008
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
50
4482
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8759
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9252
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6588
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.