[TIMOB-14083] Math.random() needs to be fixed
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | n/a |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | n/a |
Labels | math, modules, performance, random, titanium |
Reporter | Carter Lathrop |
Assignee | Unknown |
Created | 2013-05-31T07:17:40.000+0000 |
Updated | 2018-02-28T20:03:53.000+0000 |
Description
*Problem*
It seems that the generation of random numbers by Math.random() is not performing well. I've done an experiment where I generate random 8-char strings, and then I check the performance on Titanium, node and native iOS, and the results are quite conclusive. Find a detailed description of the experiment here: http://developer.appcelerator.com/question/153040/titanium-performance-generating-random-strings
*Test Case*
The experiment I did to test this lack of performance was the following: I coded the same random string generator algorithm in javascript and objC. This is how it looks in javascript:
function randomId (length) {
var textArray = [];
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < length; i++)
textArray.push(possible.charAt(Math.floor(Math.random() * possible.length)));
return textArray.join('');
}
*Results*
And then I perform a test where I generate one million of 8-char strings using this algorithm, I store them in an array, and I check for duplicates. The results are:
- When I run it with Titanium (iPhone simulator), I get an average of 40 repeated strings.
- When I run it with node (same javascript code), I get NO repeated strings.
- When I run it with XCode (the objC version of the algorithm) using the iPhone simulator, I get NO repeated strings.
*Conclusion*
It seems that there is an important lack of performance of the Math.random()
method in the Titanium environment.
Diego, Thanks for bringing this to our attention, sorry to hear a similar bug was overlooked in the past. I'll make sure engineering takes a look at this. I know it is a bit much to ask, but for clarity and completeness sake of the ticket would you mind filling out the pertinent information from your post into the description here? It would save a lot of time and we would appreciate the clarity and concise location of all necessary information. Regards, Carter
Done!! Please let me know if you need more details about it.
Thanks Diego! Great work on this, will pass along to engineering. Best Regards, Carter
Any news on this?