O
one man army
I think I an getting the hang of this language, but I ran into a
puzzler. I have a large array of data objects initialized at onLoad()
time in an HTML page. I send them in as numbers. I gather that things
are strings unless specifically made otherwise.
I do a search on the data, which is essentially a loop of string
compares. That works fine. So I want to optimize a little bit, and do
some basic numeric comparisons before I start the linear search.
I did not use parseInt() or parseFloat(), I just used the str - 0 trick
to force type conversion. However, something is not working.
Perhaps it really just needs the parseInt()/parseFloat(), or perhaps I
am missing a whole step somehow.
thanks for another pair of eyes
comments welcome...
==
function doDocInit() {
//...
gzSrch_DataSrc = new makeSrchArray();
if ( gzSrch_DataSrc == null ) {
window.alert( "Problem getting Srch data src" );
return;
}
//....
}
//==========================================================
function SrchDataObj( inSrch, inReal, inNeg) {
this.zdSrch = inSrch;
this.zdReal = inReal;
this.zdNeg = inNeg;
}
function makeSrchArray() {
var i = 0;
this[i++] = new SrchDataObj( 23, 99.6, -345.8 );
this[i++] = new SrchDataObj( 24, 98.7, -442.5 );
// a few thousand entries here...
this[i ] = null;
this.length = i;
}
//=======================================================
function lkupSrch( inSrchStr ) {
// lkup a valid Srch in
var i;
var found = false;
var loop = gzSrch_DataSrc.length;
if (0) {
var sInd;
var inSrchNum, tSrchNum;
// make lkup a little faster
// relies on sorted SrchDataSrc
sInd = loop/2;
inSrchNum = inSrchStr - 0; // use hack to convert to number
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( tSrchNum < tSrchNum) {
sInd = loop/4;
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = 0;
} else {
i = sInd - 1;
}
} else {
sInd = 3*(loop/4);
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = (loop/2) - 1;
} else {
i = sInd - 1;
}
}
} else {
i = 0;
}
// now loop
for ( ; i<loop; i++ ) {
if ( gzSrch_DataSrc.zdSrch == inSrchStr) {
found = true;
//window.alert( SrchFld.value);
break;
}
}
if ( found == false) {
return false;
}
gzCurSrch = i;
return true;
}
puzzler. I have a large array of data objects initialized at onLoad()
time in an HTML page. I send them in as numbers. I gather that things
are strings unless specifically made otherwise.
I do a search on the data, which is essentially a loop of string
compares. That works fine. So I want to optimize a little bit, and do
some basic numeric comparisons before I start the linear search.
I did not use parseInt() or parseFloat(), I just used the str - 0 trick
to force type conversion. However, something is not working.
Perhaps it really just needs the parseInt()/parseFloat(), or perhaps I
am missing a whole step somehow.
thanks for another pair of eyes
comments welcome...
==
function doDocInit() {
//...
gzSrch_DataSrc = new makeSrchArray();
if ( gzSrch_DataSrc == null ) {
window.alert( "Problem getting Srch data src" );
return;
}
//....
}
//==========================================================
function SrchDataObj( inSrch, inReal, inNeg) {
this.zdSrch = inSrch;
this.zdReal = inReal;
this.zdNeg = inNeg;
}
function makeSrchArray() {
var i = 0;
this[i++] = new SrchDataObj( 23, 99.6, -345.8 );
this[i++] = new SrchDataObj( 24, 98.7, -442.5 );
// a few thousand entries here...
this[i ] = null;
this.length = i;
}
//=======================================================
function lkupSrch( inSrchStr ) {
// lkup a valid Srch in
var i;
var found = false;
var loop = gzSrch_DataSrc.length;
if (0) {
var sInd;
var inSrchNum, tSrchNum;
// make lkup a little faster
// relies on sorted SrchDataSrc
sInd = loop/2;
inSrchNum = inSrchStr - 0; // use hack to convert to number
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( tSrchNum < tSrchNum) {
sInd = loop/4;
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = 0;
} else {
i = sInd - 1;
}
} else {
sInd = 3*(loop/4);
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = (loop/2) - 1;
} else {
i = sInd - 1;
}
}
} else {
i = 0;
}
// now loop
for ( ; i<loop; i++ ) {
if ( gzSrch_DataSrc.zdSrch == inSrchStr) {
found = true;
//window.alert( SrchFld.value);
break;
}
}
if ( found == false) {
return false;
}
gzCurSrch = i;
return true;
}