How do you efficiently create a list in a handler in AppleScript?

The AppleScript documentation offers the following code for efficient list compilation:

set bigList to {}
set bigListRef to a reference to bigList
set numItems to 100000
set t to (time of (current date)) --Start timing operations
repeat with n from 1 to numItems
    copy n to the end of bigListRef
end
set total to (time of (current date)) - t --End timing

Note the use of an explicit link. This works fine at the top level of the script or inside an explicit launch handler, but if you execute the exact exact code verbatim in another handler, for example:

on buildList()
    set bigList to {}
    set bigListRef to a reference to bigList
    set numItems to 100000
    set t to (time of (current date)) --Start timing operations
    repeat with n from 1 to numItems
        copy n to the end of bigListRef
    end
    set total to (time of (current date)) - t --End timing
end buildList
buildList()

it crashes, causing the error message: "It is not possible to make bigList a type reference." Why is this interrupted, and what is the correct way to efficiently create a list in a handler other than run ()?

+5
source share
3 answers

set end of l to iseems faster than copy i to end of l:

on f()
    set l to {}
    repeat with i from 1 to 100000
        set end of l to i
    end repeat
    l
end f
set t to time of (current date)
set l to f()
(time of (current date)) - t

You can also use a script object:

on f()
    script s
        property l : {}
    end script
    repeat with i from 1 to 100000
        copy i to end of l of s
    end repeat
    l of s
end f
set t to time of (current date)
set l to f()
(time of (current date)) - t

100000 , , , , script scpt:

" " "Untitled.scpt".

set l to f() , l , set l to {} script .applescript.

+1

, . , , script .

list_speed.xlsx

+1

"global bigList" buildList() . , , " " . , . " " , . .

, , . , , .

+1

All Articles