• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Change a aJSON list to an Applescript list?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Change a aJSON list to an Applescript list?


  • Subject: Re: Change a aJSON list to an Applescript list?
  • From: "Nigel Garvey" <email@hidden>
  • Date: Sun, 10 Sep 2017 20:52:19 +0100

Brian Christmas wrote on Sun, 10 Sep 2017 11:13:53 +1000:

>A brief question. If the dim, dark recesses of my scattered memory
recall
>correctly, this code is the fastest option when evaluating if an item
is in
>a list of 10,000 pre-determined codes, created in part with a seeded
random
>generator?
>
>>  set subscriptionsList to (jsonObject's
objectForKey:("subscriptions"))
>>as list
> set serialNumberIsInList to (aSerialNumber is in subscriptionsList)
>
>True?

Easy enough to test.  :)  jsonObject is an NSDictionary in that script,
so all that's needed is to create one of those from scratch with a
"subscriptions" key and an array containing 10,000 22-character strings
and then see how long it takes each method to determine if the last
string's in the array. As you'll see from the typical result at the end
of the main() handler below, sticking with ASObjC is very much faster
than either coercing the dictionary to a record or the array to a list.
But they all take less than a tenth of a second.  :)

  use AppleScript version "2.4" -- Yosemite (10.10) or later
  use framework "Foundation"

  main()

  on main()
    -- Make an NSDictionary like the one from the JSON serialization with an
array property ('subscriptions') containing 10000 unique 22-character strings.
    set jsonObject to makeDictionary(10000)
    -- Get the last item in the array, whose presence will presumably take the
longest to check below.
    set aSerialNumber to (jsonObject's valueForKey:("subscriptions"))'s
lastObject()

    -- Time different methods for determining whether the item's in the
dictionary's array.
    set timer to current application's class "NSDate"'s |date|()
    set jsonRecord to jsonObject as record
    considering case
      set aSerialNumberIsInList to (aSerialNumber is in jsonRecord's
subscriptions)
    end considering
    set coercingWholeDictionaryToRecord to -(timer's timeIntervalSinceNow())

    set timer to current application's class "NSDate"'s |date|()
    set subscriptionsList to (jsonObject's objectForKey:("subscriptions")) as
list
    considering case
      set aSerialNumberIsInList to (aSerialNumber is in subscriptionsList)
    end considering
    set coercingArrayToList to -(timer's timeIntervalSinceNow())

    set timer to current application's class "NSDate"'s |date|()
    set subscriptionsArray to jsonObject's objectForKey:("subscriptions")
    set aSerialNumberIsInList to (subscriptionsArray's
containsObject:(aSerialNumber)) as boolean
    set stayingWithASObjC to -(timer's timeIntervalSinceNow())

    return {coercingWholeDictionaryToRecord, coercingArrayToList,
stayingWithASObjC}
    --> {0.07054901123, 0.05519503355, 0.001346945763} (Typical results on my
machine.)
  end main

  on makeDictionary(n)
    script o
      property sourceCharacters : characters of
"ABCEDFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
      property collector : {}
    end script

    set subscriptions to current application's class "NSMutableSet"'s new()

    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ""

    repeat until (subscriptions's |count|() is n)
      set o's collector to {}
      repeat 22 times
        set end of o's collector to some item of o's sourceCharacters
      end repeat
      set thisSubscription to o's collector as text
      tell subscriptions to addObject:(thisSubscription)
    end repeat

    set AppleScript's text item delimiters to astid

    return current application's class "NSDictionary"'s
dictionaryWithDictionary:({action:"subscription.getall", |result|:"success",
nextPage:3, subscriptions:subscriptions's allObjects()})
  end makeDictionary


NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: 2 tricky questions
  • Next by Date: Slow code
  • Previous by thread: Re: Change a aJSON list to an Applescript list?
  • Next by thread: Mount Hard Drive Script help needed
  • Index(es):
    • Date
    • Thread