Array state will be cached in iOS 12 Safari. Is it a bug or feature?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
248
down vote

favorite
32












Today I found some problem of Array's value state in the newly released iOS 12 Safari, for example, code like this:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>iOS 12 Safari bugs</title>
<script type="text/javascript">
window.addEventListener("load", function ()

let arr = [1, 2, 3, 4, 5];
alert(arr.join());

document.querySelector("button").addEventListener("click", function ()

arr.reverse();
);
);
</script>
</head>
<body>
<button>Array.reverse()</button>
<p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>


After refreshing the page, the array's value is still reversed. Is this a bug or a feature of new Safari?




Here is a demo page. Try to use it with iOS 12 Safari:
https://cdn.miss.cat/demo/ios12-safari-bug.html










share|improve this question









New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 23




    Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
    – a_rahmanshah
    19 hours ago






  • 26




    macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
    – Kevin Gimbel
    17 hours ago






  • 3




    when someone ask if it's a feature or a bug.... is always the second:)
    – Taioli Francesco
    14 hours ago






  • 1




    @TaioliFrancesco unless its something by Bethesda :P
    – Christopher Nuccio
    11 hours ago















up vote
248
down vote

favorite
32












Today I found some problem of Array's value state in the newly released iOS 12 Safari, for example, code like this:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>iOS 12 Safari bugs</title>
<script type="text/javascript">
window.addEventListener("load", function ()

let arr = [1, 2, 3, 4, 5];
alert(arr.join());

document.querySelector("button").addEventListener("click", function ()

arr.reverse();
);
);
</script>
</head>
<body>
<button>Array.reverse()</button>
<p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>


After refreshing the page, the array's value is still reversed. Is this a bug or a feature of new Safari?




Here is a demo page. Try to use it with iOS 12 Safari:
https://cdn.miss.cat/demo/ios12-safari-bug.html










share|improve this question









New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 23




    Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
    – a_rahmanshah
    19 hours ago






  • 26




    macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
    – Kevin Gimbel
    17 hours ago






  • 3




    when someone ask if it's a feature or a bug.... is always the second:)
    – Taioli Francesco
    14 hours ago






  • 1




    @TaioliFrancesco unless its something by Bethesda :P
    – Christopher Nuccio
    11 hours ago













up vote
248
down vote

favorite
32









up vote
248
down vote

favorite
32






32





Today I found some problem of Array's value state in the newly released iOS 12 Safari, for example, code like this:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>iOS 12 Safari bugs</title>
<script type="text/javascript">
window.addEventListener("load", function ()

let arr = [1, 2, 3, 4, 5];
alert(arr.join());

document.querySelector("button").addEventListener("click", function ()

arr.reverse();
);
);
</script>
</head>
<body>
<button>Array.reverse()</button>
<p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>


After refreshing the page, the array's value is still reversed. Is this a bug or a feature of new Safari?




Here is a demo page. Try to use it with iOS 12 Safari:
https://cdn.miss.cat/demo/ios12-safari-bug.html










share|improve this question









New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Today I found some problem of Array's value state in the newly released iOS 12 Safari, for example, code like this:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>iOS 12 Safari bugs</title>
<script type="text/javascript">
window.addEventListener("load", function ()

let arr = [1, 2, 3, 4, 5];
alert(arr.join());

document.querySelector("button").addEventListener("click", function ()

arr.reverse();
);
);
</script>
</head>
<body>
<button>Array.reverse()</button>
<p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>


After refreshing the page, the array's value is still reversed. Is this a bug or a feature of new Safari?




Here is a demo page. Try to use it with iOS 12 Safari:
https://cdn.miss.cat/demo/ios12-safari-bug.html







javascript ios safari ios12






share|improve this question









New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 15 mins ago









Peter Mortensen

13k1983111




13k1983111






New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









abelyao

306237




306237




New contributor




abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






abelyao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 23




    Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
    – a_rahmanshah
    19 hours ago






  • 26




    macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
    – Kevin Gimbel
    17 hours ago






  • 3




    when someone ask if it's a feature or a bug.... is always the second:)
    – Taioli Francesco
    14 hours ago






  • 1




    @TaioliFrancesco unless its something by Bethesda :P
    – Christopher Nuccio
    11 hours ago













  • 23




    Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
    – a_rahmanshah
    19 hours ago






  • 26




    macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
    – Kevin Gimbel
    17 hours ago






  • 3




    when someone ask if it's a feature or a bug.... is always the second:)
    – Taioli Francesco
    14 hours ago






  • 1




    @TaioliFrancesco unless its something by Bethesda :P
    – Christopher Nuccio
    11 hours ago








23




23




Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
– a_rahmanshah
19 hours ago




Bug confirmed also in macOS 10.14 Mojave - i.imgur.com/ZJtJJC1.png
– a_rahmanshah
19 hours ago




26




26




macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
– Kevin Gimbel
17 hours ago




macOS 10.13.6 (High Sierra) with Safari Version 12.0 (13606.2.11) has the same issue. Array is still reversed after refreshing the page.
– Kevin Gimbel
17 hours ago




3




3




when someone ask if it's a feature or a bug.... is always the second:)
– Taioli Francesco
14 hours ago




when someone ask if it's a feature or a bug.... is always the second:)
– Taioli Francesco
14 hours ago




1




1




@TaioliFrancesco unless its something by Bethesda :P
– Christopher Nuccio
11 hours ago





@TaioliFrancesco unless its something by Bethesda :P
– Christopher Nuccio
11 hours ago













3 Answers
3






active

oldest

votes

















up vote
169
down vote













It's definitely a BUG! And it's a very serious bug.



Per my testing, the bug is due to the optimization of array initializers in which all values are primitive literals. For example, () => [1, null, 'x'] will result in such an array; all returned array references from this lambda will link to the same memory address, and some method like toString() will be cached. Normally, any mutable operation on such arrays will copy the data to a separate memory space and link to it; this is called copy-on-write, or CoW for short.



The reverse() method mutates the array, so it should trigger a copy-on-write. Apparently, it no longer does so, which causes the bug you’re seeing.



On the other hand, all methods which do not modify the array should not trigger CoW, and I’ve found that even a.fill(value, 0, 0) or a.copyWithin(index, 0, 0) won't trigger CoW because such callings don't really mutate the array. But I noticed that a.slice() WILL trigger CoW. So, if I had to guess, I’d say the underlying cause of this bug may be that someone accidentally swapped the indices of slice and reverse.My guess is not correct, the original author (Keith Miller @ Apple) just simply miss the reverse() case though he already wrote many test cases. slice() is a separate story, may be related to fastSlice optimization though I still don't understand the reason. Anyway, unnecessary CoW won't cause bug.






share|improve this answer


















  • 7




    Note: Safari 12.0 on Mac OS X also have the same issue.
    – hax
    16 hours ago






  • 5




    Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
    – sideshowbarker
    16 hours ago







  • 3




    I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
    – Zenexer
    13 hours ago







  • 1




    @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
    – hax
    13 hours ago

















up vote
33
down vote













I wrote a lib to fix the bug.
https://www.npmjs.com/package/array-reverse-polyfill



This is the code:






(function() 
function buggy()
var a = [1, 2];
return String(a) === String(a.reverse());

if(!buggy()) return;
var r = Array.prototype.reverse;
Array.prototype.reverse = function reverse()
if (Array.isArray(this)) this.length = this.length;
return r.call(this);

)();








share|improve this answer


















  • 2




    Update at any time. Welcome to contribute.
    – Edire Fan
    16 hours ago






  • 2




    @EdireFan why does this work?
    – zephi
    7 hours ago






  • 1




    @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
    – Cœur
    2 hours ago

















up vote
0
down vote













It seems not to be cached if the number of elements changes.

I was able to avoid this like this.






<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>iOS 12 Safari bugs</title>
<script type="text/javascript">
window.addEventListener("load", function ()

let arr = [1, 2, 3, 4, 5];
arr.push('');
arr.pop();
alert(arr.join());

document.querySelector("button").addEventListener("click", function ()

arr.reverse();
);
);
</script>
</head>
<body>
<button>Array.reverse()</button>
<p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>







share








New contributor




Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    abelyao is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52390368%2farray-state-will-be-cached-in-ios-12-safari-is-it-a-bug-or-feature%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    169
    down vote













    It's definitely a BUG! And it's a very serious bug.



    Per my testing, the bug is due to the optimization of array initializers in which all values are primitive literals. For example, () => [1, null, 'x'] will result in such an array; all returned array references from this lambda will link to the same memory address, and some method like toString() will be cached. Normally, any mutable operation on such arrays will copy the data to a separate memory space and link to it; this is called copy-on-write, or CoW for short.



    The reverse() method mutates the array, so it should trigger a copy-on-write. Apparently, it no longer does so, which causes the bug you’re seeing.



    On the other hand, all methods which do not modify the array should not trigger CoW, and I’ve found that even a.fill(value, 0, 0) or a.copyWithin(index, 0, 0) won't trigger CoW because such callings don't really mutate the array. But I noticed that a.slice() WILL trigger CoW. So, if I had to guess, I’d say the underlying cause of this bug may be that someone accidentally swapped the indices of slice and reverse.My guess is not correct, the original author (Keith Miller @ Apple) just simply miss the reverse() case though he already wrote many test cases. slice() is a separate story, may be related to fastSlice optimization though I still don't understand the reason. Anyway, unnecessary CoW won't cause bug.






    share|improve this answer


















    • 7




      Note: Safari 12.0 on Mac OS X also have the same issue.
      – hax
      16 hours ago






    • 5




      Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
      – sideshowbarker
      16 hours ago







    • 3




      I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
      – Zenexer
      13 hours ago







    • 1




      @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
      – hax
      13 hours ago














    up vote
    169
    down vote













    It's definitely a BUG! And it's a very serious bug.



    Per my testing, the bug is due to the optimization of array initializers in which all values are primitive literals. For example, () => [1, null, 'x'] will result in such an array; all returned array references from this lambda will link to the same memory address, and some method like toString() will be cached. Normally, any mutable operation on such arrays will copy the data to a separate memory space and link to it; this is called copy-on-write, or CoW for short.



    The reverse() method mutates the array, so it should trigger a copy-on-write. Apparently, it no longer does so, which causes the bug you’re seeing.



    On the other hand, all methods which do not modify the array should not trigger CoW, and I’ve found that even a.fill(value, 0, 0) or a.copyWithin(index, 0, 0) won't trigger CoW because such callings don't really mutate the array. But I noticed that a.slice() WILL trigger CoW. So, if I had to guess, I’d say the underlying cause of this bug may be that someone accidentally swapped the indices of slice and reverse.My guess is not correct, the original author (Keith Miller @ Apple) just simply miss the reverse() case though he already wrote many test cases. slice() is a separate story, may be related to fastSlice optimization though I still don't understand the reason. Anyway, unnecessary CoW won't cause bug.






    share|improve this answer


















    • 7




      Note: Safari 12.0 on Mac OS X also have the same issue.
      – hax
      16 hours ago






    • 5




      Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
      – sideshowbarker
      16 hours ago







    • 3




      I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
      – Zenexer
      13 hours ago







    • 1




      @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
      – hax
      13 hours ago












    up vote
    169
    down vote










    up vote
    169
    down vote









    It's definitely a BUG! And it's a very serious bug.



    Per my testing, the bug is due to the optimization of array initializers in which all values are primitive literals. For example, () => [1, null, 'x'] will result in such an array; all returned array references from this lambda will link to the same memory address, and some method like toString() will be cached. Normally, any mutable operation on such arrays will copy the data to a separate memory space and link to it; this is called copy-on-write, or CoW for short.



    The reverse() method mutates the array, so it should trigger a copy-on-write. Apparently, it no longer does so, which causes the bug you’re seeing.



    On the other hand, all methods which do not modify the array should not trigger CoW, and I’ve found that even a.fill(value, 0, 0) or a.copyWithin(index, 0, 0) won't trigger CoW because such callings don't really mutate the array. But I noticed that a.slice() WILL trigger CoW. So, if I had to guess, I’d say the underlying cause of this bug may be that someone accidentally swapped the indices of slice and reverse.My guess is not correct, the original author (Keith Miller @ Apple) just simply miss the reverse() case though he already wrote many test cases. slice() is a separate story, may be related to fastSlice optimization though I still don't understand the reason. Anyway, unnecessary CoW won't cause bug.






    share|improve this answer














    It's definitely a BUG! And it's a very serious bug.



    Per my testing, the bug is due to the optimization of array initializers in which all values are primitive literals. For example, () => [1, null, 'x'] will result in such an array; all returned array references from this lambda will link to the same memory address, and some method like toString() will be cached. Normally, any mutable operation on such arrays will copy the data to a separate memory space and link to it; this is called copy-on-write, or CoW for short.



    The reverse() method mutates the array, so it should trigger a copy-on-write. Apparently, it no longer does so, which causes the bug you’re seeing.



    On the other hand, all methods which do not modify the array should not trigger CoW, and I’ve found that even a.fill(value, 0, 0) or a.copyWithin(index, 0, 0) won't trigger CoW because such callings don't really mutate the array. But I noticed that a.slice() WILL trigger CoW. So, if I had to guess, I’d say the underlying cause of this bug may be that someone accidentally swapped the indices of slice and reverse.My guess is not correct, the original author (Keith Miller @ Apple) just simply miss the reverse() case though he already wrote many test cases. slice() is a separate story, may be related to fastSlice optimization though I still don't understand the reason. Anyway, unnecessary CoW won't cause bug.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 13 hours ago

























    answered yesterday









    hax

    4841310




    4841310







    • 7




      Note: Safari 12.0 on Mac OS X also have the same issue.
      – hax
      16 hours ago






    • 5




      Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
      – sideshowbarker
      16 hours ago







    • 3




      I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
      – Zenexer
      13 hours ago







    • 1




      @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
      – hax
      13 hours ago












    • 7




      Note: Safari 12.0 on Mac OS X also have the same issue.
      – hax
      16 hours ago






    • 5




      Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
      – sideshowbarker
      16 hours ago







    • 3




      I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
      – Zenexer
      13 hours ago







    • 1




      @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
      – hax
      13 hours ago







    7




    7




    Note: Safari 12.0 on Mac OS X also have the same issue.
    – hax
    16 hours ago




    Note: Safari 12.0 on Mac OS X also have the same issue.
    – hax
    16 hours ago




    5




    5




    Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
    – sideshowbarker
    16 hours ago





    Yeah it’s been fixed in the sources already, and shipped in Safari Technology Preview already. Try cdn.miss.cat/demo/ios12-safari-bug.html in Safari Technology Preview 65. You’ll find that it doesn’t have the bug.
    – sideshowbarker
    16 hours ago





    3




    3




    I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
    – Zenexer
    13 hours ago





    I don’t believe the underlying cause of the bug is the result of an index mixup; instead, it seems to be caused by neglecting to check whether an object is immutable before modifying it. The slice issue may have a similar explanation, but it’s not the same but and won’t be fixed by the patch for reverse, as far as I can tell. You should consider opening a WebKit bug report for the slice issue.
    – Zenexer
    13 hours ago





    1




    1




    @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
    – hax
    13 hours ago




    @Zenexer You are right. I wrote this answer before I found the bugs.webkit.org/show_bug.cgi?id=188794 and see the source code. I will edit my answer.
    – hax
    13 hours ago












    up vote
    33
    down vote













    I wrote a lib to fix the bug.
    https://www.npmjs.com/package/array-reverse-polyfill



    This is the code:






    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();








    share|improve this answer


















    • 2




      Update at any time. Welcome to contribute.
      – Edire Fan
      16 hours ago






    • 2




      @EdireFan why does this work?
      – zephi
      7 hours ago






    • 1




      @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
      – Cœur
      2 hours ago














    up vote
    33
    down vote













    I wrote a lib to fix the bug.
    https://www.npmjs.com/package/array-reverse-polyfill



    This is the code:






    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();








    share|improve this answer


















    • 2




      Update at any time. Welcome to contribute.
      – Edire Fan
      16 hours ago






    • 2




      @EdireFan why does this work?
      – zephi
      7 hours ago






    • 1




      @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
      – Cœur
      2 hours ago












    up vote
    33
    down vote










    up vote
    33
    down vote









    I wrote a lib to fix the bug.
    https://www.npmjs.com/package/array-reverse-polyfill



    This is the code:






    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();








    share|improve this answer














    I wrote a lib to fix the bug.
    https://www.npmjs.com/package/array-reverse-polyfill



    This is the code:






    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();








    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();





    (function() 
    function buggy()
    var a = [1, 2];
    return String(a) === String(a.reverse());

    if(!buggy()) return;
    var r = Array.prototype.reverse;
    Array.prototype.reverse = function reverse()
    if (Array.isArray(this)) this.length = this.length;
    return r.call(this);

    )();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 9 hours ago









    Community♦

    11




    11










    answered yesterday









    Edire Fan

    26127




    26127







    • 2




      Update at any time. Welcome to contribute.
      – Edire Fan
      16 hours ago






    • 2




      @EdireFan why does this work?
      – zephi
      7 hours ago






    • 1




      @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
      – Cœur
      2 hours ago












    • 2




      Update at any time. Welcome to contribute.
      – Edire Fan
      16 hours ago






    • 2




      @EdireFan why does this work?
      – zephi
      7 hours ago






    • 1




      @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
      – Cœur
      2 hours ago







    2




    2




    Update at any time. Welcome to contribute.
    – Edire Fan
    16 hours ago




    Update at any time. Welcome to contribute.
    – Edire Fan
    16 hours ago




    2




    2




    @EdireFan why does this work?
    – zephi
    7 hours ago




    @EdireFan why does this work?
    – zephi
    7 hours ago




    1




    1




    @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
    – Cœur
    2 hours ago




    @zephi, I guess that writing on length (this.length = this.length) will trigger Copy On Write, so will change the memory address of the array, and so will fix the behavior of reverse.
    – Cœur
    2 hours ago










    up vote
    0
    down vote













    It seems not to be cached if the number of elements changes.

    I was able to avoid this like this.






    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>iOS 12 Safari bugs</title>
    <script type="text/javascript">
    window.addEventListener("load", function ()

    let arr = [1, 2, 3, 4, 5];
    arr.push('');
    arr.pop();
    alert(arr.join());

    document.querySelector("button").addEventListener("click", function ()

    arr.reverse();
    );
    );
    </script>
    </head>
    <body>
    <button>Array.reverse()</button>
    <p style="color:red;">test: click button and refresh page, code:</p>
    </body>
    </html>







    share








    New contributor




    Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote













      It seems not to be cached if the number of elements changes.

      I was able to avoid this like this.






      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
      <title>iOS 12 Safari bugs</title>
      <script type="text/javascript">
      window.addEventListener("load", function ()

      let arr = [1, 2, 3, 4, 5];
      arr.push('');
      arr.pop();
      alert(arr.join());

      document.querySelector("button").addEventListener("click", function ()

      arr.reverse();
      );
      );
      </script>
      </head>
      <body>
      <button>Array.reverse()</button>
      <p style="color:red;">test: click button and refresh page, code:</p>
      </body>
      </html>







      share








      New contributor




      Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        0
        down vote










        up vote
        0
        down vote









        It seems not to be cached if the number of elements changes.

        I was able to avoid this like this.






        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>iOS 12 Safari bugs</title>
        <script type="text/javascript">
        window.addEventListener("load", function ()

        let arr = [1, 2, 3, 4, 5];
        arr.push('');
        arr.pop();
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()

        arr.reverse();
        );
        );
        </script>
        </head>
        <body>
        <button>Array.reverse()</button>
        <p style="color:red;">test: click button and refresh page, code:</p>
        </body>
        </html>







        share








        New contributor




        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        It seems not to be cached if the number of elements changes.

        I was able to avoid this like this.






        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>iOS 12 Safari bugs</title>
        <script type="text/javascript">
        window.addEventListener("load", function ()

        let arr = [1, 2, 3, 4, 5];
        arr.push('');
        arr.pop();
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()

        arr.reverse();
        );
        );
        </script>
        </head>
        <body>
        <button>Array.reverse()</button>
        <p style="color:red;">test: click button and refresh page, code:</p>
        </body>
        </html>








        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>iOS 12 Safari bugs</title>
        <script type="text/javascript">
        window.addEventListener("load", function ()

        let arr = [1, 2, 3, 4, 5];
        arr.push('');
        arr.pop();
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()

        arr.reverse();
        );
        );
        </script>
        </head>
        <body>
        <button>Array.reverse()</button>
        <p style="color:red;">test: click button and refresh page, code:</p>
        </body>
        </html>





        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>iOS 12 Safari bugs</title>
        <script type="text/javascript">
        window.addEventListener("load", function ()

        let arr = [1, 2, 3, 4, 5];
        arr.push('');
        arr.pop();
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()

        arr.reverse();
        );
        );
        </script>
        </head>
        <body>
        <button>Array.reverse()</button>
        <p style="color:red;">test: click button and refresh page, code:</p>
        </body>
        </html>





        share








        New contributor




        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share






        New contributor




        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 6 mins ago









        Atsushi Sasaki

        1




        1




        New contributor




        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Atsushi Sasaki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















            abelyao is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            abelyao is a new contributor. Be nice, and check out our Code of Conduct.












            abelyao is a new contributor. Be nice, and check out our Code of Conduct.











            abelyao is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52390368%2farray-state-will-be-cached-in-ios-12-safari-is-it-a-bug-or-feature%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke