NSMutableArray: Any way to speed this up?

Is there a secret to efficiently dealing with “large” NSMutableArrays in AppleScript? Something like the following (silly example) appears to run in a fraction of a second when written as Objective C but takes about half a minute in AppleScript.

use framework "Foundation"

property NSMutableArray : a reference to current application's NSMutableArray

set menuItems to NSMutableArray's arrayWithArray:{}

repeat with i from 0 to 30000
(menuItems's insertObject:"test" atIndex:i)
end repeat

set newMenuItems to NSMutableArray's array()

repeat with i from 0 to (menuItems's |count|()) - 1
(newMenuItems's addObject:(menuItems's objectAtIndex:i))
end repeat

Hi.

Since you’re using an AppleScript repeat, build a large AppleScript list rather than a large NSMutableArray. Then convert afterwards:

use framework "Foundation"

property NSMutableArray : a reference to current application's NSMutableArray

set menuItems to {}

repeat with i from 0 to 30000
	set end of my menuItems to i -- 'my' is equivalent to using 'a reference to' menuItems.
end repeat

set menuItems to NSMutableArray's arrayWithArray:menuItems
set newMenuItems to menuItems's mutableCopy()
3 Likes