Skip to content

Instantly share code, notes, and snippets.

@bterlson
Last active August 29, 2015 13:57
Show Gist options
  • Save bterlson/9838408 to your computer and use it in GitHub Desktop.
Save bterlson/9838408 to your computer and use it in GitHub Desktop.
The tweak is to only do Get if HasProperty returns true.
1. Let O be the result of calling ToObject passing the this value as the argument.
2. ReturnIfAbrupt(O).
3. Let lenVal be the result of Get(O, "length").
4. Let len be ToLength(lenVal).
5. ReturnIfAbrupt(len).
6. Let middle be floor(len/2).
7. Let lower be 0.
8. Repeat, while lower ≠ middle
a. Let upper be len−lower−1.
b. Let upperP be ToString(upper).
c. Let lowerP be ToString(lower).
d. Let lowerExists be the result of HasProperty(O, lowerP).
e. ReturnIfAbrupt(lowerExists).
f. If lowerExists is true, then
i. Let lowerValue be the result of Get(O, lowerP).
ii. ReturnIfAbrupt(lowerValue).
g. Let upperExists be the result of HasProperty(O, upperP).
h. ReturnIfAbrupt(upperExists).
i. If upperExists is true, then
i. Let upperValue be the result of Get(O, upper).
ii. ReturnIfAbrupt(upperValue).
j. If lowerExists is true and upperExists is true, then
i. Let putStatus be the result of Put(O, lowerP, upperValue, true).
ii. ReturnIfAbrupt(putStatus).
iii. Let putStatus be the result of Put(O, upperP, lowerValue, true).
iv. ReturnIfAbrupt(putStatus).
k. Else if lowerExists is false and upperExists is true, then
i. Let putStatus be the result of Put(O, lowerP, upperValue, true).
ii. ReturnIfAbrupt(putStatus).
iii. Let deleteStatus be the result of DeletePropertyOrThrow (O, upperP).
iv. ReturnIfAbrupt(deleteStatus).
l. Else if lowerExists is true and upperExists is false, then
i. Let deleteStatus be the result of DeletePropertyOrThrow (O, lowerP).
ii. ReturnIfAbrupt(deleteStatus).
iii. Let putStatus be the result of Put(O, upperP, lowerValue, true).
iv. ReturnIfAbrupt(putStatus).
m. Else both lowerExists and upperExists are false
i.No action is required.
n. Increase lower by 1.
9. Return O .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment