daily pastebin goal
57%
SHARE
TWEET

Instance.new() with parent argument

einsteinK Nov 1st, 2016 247 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local instance_new,queue = Instance.new
  3. local mutex = Instance.new("BindableEvent")
  4. coroutine.resume(coroutine.create(function()
  5.     while true do
  6.         mutex.Event:wait()
  7.         coroutine.yield()
  8.         for obj,parent in pairs(queue) do
  9.             obj.Parent = parent
  10.         end queue = nil
  11.     end
  12. end))
  13. Instance = {
  14.     new = function(class,parent)
  15.         local obj = instance_new(class)
  16.         if not parent then return obj end
  17.         if queue then
  18.             queue[obj] = parent
  19.         else
  20.             queue = {[obj]=parent}
  21.             mutex:Fire()
  22.         end return obj
  23.     end;
  24. }
  25.  
  26. -- localize, otherwise indexing might influence results
  27. local oldNew,newNew = instance_new,Instance.new
  28. -- easier to clean up
  29. local model = Instance.new("Folder",workspace)
  30. -- needed for a quick test at the bottom
  31. local wall
  32.  
  33. local t = tick()
  34. for i=1,100 do
  35.     wall = oldNew("Part")
  36.     wall.Anchored = true
  37.     wall.Size = Vector3.new(10,10,1)
  38.     wall.CFrame = CFrame.new()
  39.     wall.Parent = model
  40. end
  41. print(tick()-t)
  42.  
  43. model:ClearAllChildren() wait(1) -- clean up
  44.  
  45. local t = tick()
  46. for i=1,100 do
  47.     wall = oldNew("Part",model)
  48.     wall.Anchored = true
  49.     wall.Size = Vector3.new(10,10,1)
  50.     wall.CFrame = CFrame.new()
  51. end
  52. print(tick()-t)
  53.  
  54. model:ClearAllChildren() wait(1) -- clean up
  55.  
  56. local t = tick()
  57. for i=1,100 do
  58.     wall = newNew("Part",model)
  59.     wall.Anchored = true
  60.     wall.Size = Vector3.new(10,10,1)
  61.     wall.CFrame = CFrame.new()
  62. end
  63. print(tick()-t)
  64.  
  65. local diff = tick()-t -- correction below
  66.  
  67. print(wall.Parent)
  68. local t = tick()
  69. wall.Changed:wait()
  70. print(wall.Parent,tick()-t)
  71.  
  72. wait() model:Destroy() -- clean up
  73.  
  74. --[[ Output (ran several times)
  75. 0.00055575370788574
  76. 0.079145908355713
  77. 0.00058746337890625
  78. nil
  79. Folder 0.0012581348419189
  80.  
  81. 0.0005803108215332
  82. 0.07579493522644
  83. 0.00054287910461426
  84. nil
  85. Folder 0.00094771385192871
  86.  
  87. 0.00053167343139648
  88. 0.078535318374634
  89. 0.00054073333740234
  90. nil
  91. Folder 0.0022518634796143
  92.  
  93. 0.00056672096252441
  94. 0.079301595687866
  95. 0.00054311752319336
  96. nil
  97. Folder 0.001467227935791
  98.  
  99. 0.00053739547729492
  100. 0.075802326202393
  101. 0.00056886672973633
  102. nil
  103. Folder 0.0021722316741943
  104. --]]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top