On Apr 2, 1:07 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
The alMIGHTY N wrote on 02 apr 2007 in comp.lang.javascript:
Hi all,
Let's say I have a simple math formula:
sum (x * y / 1000) / (sum z / 1000)
sum (x * y / 1000) / sum( z / 1000) ???
sum? That is not javascript! Is that a function?
I was just relating the math formula itself. That's obviously not
code, haha.
I have to do this across 50 items, each with an x, y and z value, when
the page first loads AND when a user modifies any of the x, y and z
values.
Needless to say, I need to have a Javascript function that handles
this formula to deal with the modifications.
However, I have a choice on page load to either have the database in
which this data is initially stored perform this calculation or to
have the same Javascript function that will handle the onchange event
handle the initial calculation as well.
a) Would it be noticeably faster to do it on the database side?
That depends on how often you want to fetch the data from the database
and where they are stored then.
The data is retrieved from the database upon page load. After that,
all calculation is done on the page. After any number of
calculations / modifications, the user can choose to save the data to
the database. That doesn't reload the page, however. Basically, the
user would have to refresh the page explicitly to load data from the
database again.
b) Would that difference be enough to warrant having redundancy on the
calculation?
What is "redundancy on the calculation"?
What I meant was to have the same calculation in two different places
- back end in the database *and* front end in the Javascript.
One of my co-workers feels that it's always better to do calculations
on the back-end period, no exceptions. My other co-worker feels that
it's bad to have the same calculation in two different places,
especially when one place is back end and the other front end, because
that adds to the list of things we need to handle for future
maintenance.
There are 3, not 2 places to handle things, if I understand your Q
enough:
1 in the serverside querystring of the database [what engine?]
2 in serverside jscript or vbscript [talking ASP]
3 javascript on the client/browser
Only two places: on the back end, there is a stored procedure in the
database that calculates the formula and then spits out the resulting
number into XML; on the front end, the HTML generated by the XSL
attached to the aforementioned XML contains Javascript that has that
calculation.
Another thing... the XML generated by the back end functions also
contains x, y and z, which is why even on the initial page load, the
Javascript could easily run the calculation instead of doing it on the
back end... the question is whether it should.
What does your co-workers understand under front and back end?
Actually, both of my co-workers are back end developers, dealing in
Java. I'm the sole front-end developer in the project.
What's your opinion on which is the better way to do things?
Depends on your preferences, your joy in coding, and if those two do not
suffice, trying out and measuring the 3 or more possible ways.
Well, we've already coded on both the back end and the front end. My
co-workers are just debating whether to use the back end code at all,
and I'm caught in the middle. :-(
Personally, I think the difference in speed is negligible if it's even
there. The page seems to load up fast no matter what option we go
with.
However, the one who believes entirely in back end coding keeps
insisting that her way is the best because the calculation on the back
end takes in the scale of milliseconds whereas the calculation on the
front end takes on the scale of seconds (or so she claims but maybe
she's just trying to defend her work haha).
Anyway, just looking for opinions, s'all. I don't know enough about
back end programming to be able to say whether it's any faster or
slower than what could be done on Javascript. I imagine it is faster
to some degree, but not to a degree that's noticeable to a user,
especially with such a simple calculation.
Cheers!
My personal favorite is to code as I like when I come to it.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)