Hi,

This is about 2/3rds to twice as fast by my measurements (in Scout). Any one 
confirm?

Changes include.
- Use a string to store hex digits and charAt rather than array of ascii
- Use Array.join rather than String.fromCharCode
- Remove Math.floor as they are not required
- Set up static array and reuse that rather than creating new array each time

The timeString could probably be improved to remove the string manipulation but 
put the digits directly into the correct place into the array to save another 
5% or so.

BYW any see where Number.toString is being called in there?

Any feedback?

Thanks,
Justin

diff --git a/frameworks/projects/framework/src/mx/utils/UIDUtil.as 
b/frameworks/projects/framework/src/mx/utils/UIDUtil.as
index 4993563..576b598 100644
--- a/frameworks/projects/framework/src/mx/utils/UIDUtil.as
+++ b/frameworks/projects/framework/src/mx/utils/UIDUtil.as
@@ -61,10 +61,16 @@ public class UIDUtil
 
     /**
      *  @private
-     *  Char codes for 0123456789ABCDEF
      */
-    private static const ALPHA_CHAR_CODES:Array = [48, 49, 50, 51, 52, 53, 54, 
-        55, 56, 57, 65, 66, 67, 68, 69, 70];
+       private static const CHAR_CODES:String = "0123456789ABCDEF";
+       
+       private static var EMPTYUID:Array = [
+               '0','0','0','0','0','0','0','0',
+               '-','0','0','0','0',
+               '-','0','0','0','0',
+               '-','0','0','0','0',
+               '0','0','0','0','0','0','0','0','0','0','0','0'
+       ];
 
     
//--------------------------------------------------------------------------
     //
@@ -106,53 +112,53 @@ public class UIDUtil
      *  @playerversion AIR 1.1
      *  @productversion Flex 3
      */
-    public static function createUID():String
-    {
-        var uid:Array = new Array(36);
-        var index:int = 0;
-        
-        var i:int;
-        var j:int;
-        
-        for (i = 0; i < 8; i++)
-        {
-            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
-        }
-
-        for (i = 0; i < 3; i++)
-        {
-            uid[index++] = 45; // charCode for "-"
-            
-            for (j = 0; j < 4; j++)
-            {
-                uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  
16)];
-            }
-        }
-        
-        uid[index++] = 45; // charCode for "-"
-
-        var time:Number = new Date().getTime();
-        // Note: time is the number of milliseconds since 1970,
-        // which is currently more than one trillion.
-        // We use the low 8 hex digits of this number in the UID.
-        // Just in case the system clock has been reset to
-        // Jan 1-4, 1970 (in which case this number could have only
-        // 1-7 hex digits), we pad on the left with 7 zeros
-        // before taking the low digits.
-        var timeString:String = ("0000000" + 
time.toString(16).toUpperCase()).substr(-8);
-        
-        for (i = 0; i < 8; i++)
-        {
-            uid[index++] = timeString.charCodeAt(i);
-        }
-        
-        for (i = 0; i < 4; i++)
-        {
-            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
-        }
-        
-        return String.fromCharCode.apply(null, uid);
-    }
+       public static function createUID():String
+       {
+               var uid:Array = EMPTYUID;
+               var index:int = 0;
+               
+               var i:int;
+               var j:int;
+               
+               for (i = 0; i < 8; i++)
+               {
+                       uid[index++] = CHAR_CODES.charAt(Math.random() * 16);
+               }
+               
+               for (i = 0; i < 3; i++)
+               {
+                       index++; // skip "-"
+                       
+                       for (j = 0; j < 4; j++)
+                       {
+                               uid[index++] = CHAR_CODES.charAt(Math.random() 
* 16);
+                       }
+               }
+               
+               index++; // skip "-"
+               
+               var time:Number = new Date().getTime();
+               // Note: time is the number of milliseconds since 1970,
+               // which is currently more than one trillion.
+               // We use the low 8 hex digits of this number in the UID.
+               // Just in case the system clock has been reset to
+               // Jan 1-4, 1970 (in which case this number could have only
+               // 1-7 hex digits), we pad on the left with 7 zeros
+               // before taking the low digits.
+               var timeString:String = ("0000000" + 
time.toString(16).toUpperCase()).substr(-8);
+               
+               for (i = 0; i < 8; i++)
+               {
+                       uid[index++] = timeString.charAt(i);
+               }
+               
+               for (i = 0; i < 4; i++)
+               {
+                       uid[index++] = CHAR_CODES.charAt(Math.random() * 16);
+               }
+               
+               return uid.join("");
+       }


Reply via email to