Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created August 3, 2020 05:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/7f13f4297d7307a47ad78f0254ce3353 to your computer and use it in GitHub Desktop.
Save tanaikech/7f13f4297d7307a47ad78f0254ce3353 to your computer and use it in GitHub Desktop.
IMPORTANT: reduceRight with and without v8 runtime for Google Apps Script

IMPORTANT: reduceRight with and without v8 runtime for Google Apps Script

This is an important point for using reduceRignt with and without v8 runtime for Google Apps Script.

Sample script

function myFunction() {
  var array = ["a", "b", "c", "d", "e"];
  var res = array.reduceRight(function (ar, e, i) {
    ar.push([e, i]);
    return ar;
  }, []);
  Logger.log(res);
}

Result

With V8

When V8 runtime is used, the following result is obtained.

[["e",4],["d",3],["c",2],["b",1],["a",0]]

Without V8

When V8 runtime is NOT used, the following result is obtained.

[["e",0],["d",1],["c",2],["b",3],["a",4]]

Summary

When above results are compared, it is found that the indexes are different. The indexes of "With V8" is in the opposite direction to that of "Without V8". Please be careful this.

When I used the script including reduceRight for Rhino runtime (without V8) with enabling V8 runtime, an error occurs and I had spent for the time to find the reason due to reduceRight. So I would like to introduce this as an important point.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment