Speed May Kill Your Code
When I started on this internet journey in 2000, I never considered what would happen to our code if things started executing faster than milliseconds. Nearly 100% of the JavaScript code and the corresponding UniBasic code for Universe and Unidata was never envisioned to run in nanoseconds.
One of the many programmers I worked with over the years thought that combining the payroll subroutines and calculations into a single program of about 10K plus lines would make it run faster. The program spent much of its time cycling in and out of memory it was a boatload slower than the original routines that called each other.
Then there was the time I had about 800K of records and the SELECT process on the Unidata database took about 20 minutes in the late 1990's on our 1MB machine. Another example was I had about 400K records that were on a Universe distributed file of 8 parts that took about an hour to select.
Just the other day I had a piece of code in our CRM system that keeps track of what times the customer is open that had stopped working. It was not noticed because the client that used the code was no longer a client and another client asked what was on the hourly tab. The datatable only displayed a single value instead of 7 values for each date.
The first incarnation of this code was written totally in JavaScript about 10 years ago. Since then it has been used for about 7 years and no problems were detected from the client or our QA department. Moreover, when I converted from dhtmlX to the Webix JavaScript library the code was modified to work with the new Webix library. Here is the dhtmlX Code:
grid_4.addRow((new Date()).valueOf(),"Sun,,,,,,,,,,,,,,,,,,,,,,,,","-1");
Here is the Webix Code with a slight modification of zeroes for all positions of columns that we are adding.
var jsonData = {};
for (var k in jsonColumns) {
var columnName = jsonColumns[k];
createJson(jsonData,columnName, "0");
}
$$("Table_4").add({"id":(new Date()).valueOf(),"G4Wk":"Sun",jsonData});
There are 6 more rows of data that are not shown in the above examples for Mon, Tue, Wed, Thu, Fri, and Sat. The JavaScript "Date valueOf()" method is the problem child here. According to the documentation on the Mozilla site, the data returned from this method is the "...number of milliseconds for this data since epoch..." with one millisecond resolution.
领英推荐
Each execution of the table adds a line with the Date().valueOf() returns the same milliseconds for all seven calls. This means that the code only writes out a single line instead of 7 for each day of the week. Having tested this on Chrome, Firefox, Edge, and Safari and all of them failed because they now run this in under a millisecond.
The solution I came up with is neither elegant nor particularly smart but it works for now.
$$("Table_4").add({"id":(new Date()).valueOf()+1,"G4Wk":"Sun",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+2,"G4Wk":"Mon",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+3,"G4Wk":"Tue",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+4,"G4Wk":"Wed",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+5,"G4Wk":"Thu",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+6,"G4Wk":"Fri",jsonData});
$$("Table_4").add({"id":(new Date()).valueOf()+7,"G4Wk":"Sat",jsonData});
In our UniBasic code to get a unique number I have used the following code for Universe and Unidata. Unidata requires that I convert the time to have decimals to match up with how Universe displays the "SYSTEM(12)" command with three decimals.
UTC.TIME = SYSTEM(12)
IF NOT(UNIVERSE) THEN UTC.TIME = OCONV(UTC.TIME,'MD3')
UNIQUE.NO = DATE():UTC.TIME
This now fails occasionally as well because some of our calls through U2WebLink using UniObjects for Java to get to UniBasic subroutines are happening in under a millisecond and this variable on the last line is not unique. Here is the code that was needed to fix the problem that was again neither elegant nor particularly smart.
UTC.TIME = SYSTEM(12)
IF NOT(UNIVERSE) THEN UTC.TIME = OCONV(UTC.TIME,'MD3')
UNIQUE.NO = DATE():UTC.TIME:RND(1000)
When I started learning R83 Pick Basic, Prime Information, Universe, Unidata, and all of the others I did not think I would ever have to address speed issues where my code was running too fast. Those days of awe that my code even ran let alone executed faster than a millisecond are now in front of all of us. As I go through the hundreds of thousands of lines of code to make sure I have no other gotchas when the program executes in under a millisecond, I wish you all good hunting.