Xah Talk Show 2019-10-31 JavaScript npm webpack live coding, 90min rant on modern JavaScript

vidthumb iIDd9zOiy1M

topics talked:

"use strict";

/* [
xah_is_array_equal(array1, array2)
Return true if 2 array are equal
Allow array-like object
Allow nested array

JavaScript: Test Object Equality 🚀
http://xahlee.info/js/js_test_object_equality.html

version 2019-04-24
 ] */
const xah_is_array_equal = ((array1, array2) => {
  // allow array-like object
  if (Array.isArray(array1) !== Array.isArray(array2)) return false;
  if (array1.length !== array2.length) return false;

  return Array.prototype.every.call(
    array1,
    (x, i) => {
      const y = array2[i];
      if (Array.isArray(x)) {
        if (!Array.isArray(y)) {
          return false;
        } else {
          return xah_is_array_equal(x, y);
        }
      } else if (typeof x === "object" && typeof x !== null) {
        if (!(typeof y === "object" && typeof y !== null)) {
          return false;
        } else {
          return xah_is_obj_equal(x, y);
        }
      } else {
        return (x === y);
      }
    },
  );
});

function flat_jon(arr, res) {
  var i = 0, cur;
  var len = arr.length;
  for (; i < len; i++) {
    cur = arr[i];
    Array.isArray(cur) ? flat_jon(cur, res) : res.push(cur);
  }
  return res;
}

const flat_xah = ((array1, n) => {
  if (n === undefined) n = 1;
  if (
    n > 0 &&
    Array.prototype.some.call(array1, Array.isArray)
  ) {
    return flat_xah(Array.prototype.concat.apply([], array1), n - 1);
  } else {
    return array1;
  }
});

// -----------------------------------
// test

// const aa = [0,[1,[2,[3,[4,[5,[6]]]]]], "xyz"];

const aa = [[["a", ["b", ["k", ["a", ["b", ["c"], [["a", [["a", ["b", ["k", [
  "a",
  ["b", ["c"]],
  ["a", ["x", ["c"], ["a", ["x", ["k"]]], ["d", ["z"]]]],
  ["d", ["m"]],
], ["d", ["e"]]]]], ["d", ["e"]]], ["b", ["k", ["a", ["b", ["c"]], ["a", [
  "x",
  ["c"],
  ["a", ["x", ["k"]]],
  ["d", ["z"]],
]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["a", ["x", ["c"], ["a", [
  "x",
  ["k"],
], [["a", ["b", ["k", ["a", ["b", ["c"]], ["a", ["x", ["c"], ["a", ["x", [
  "k",
]]], ["d", ["z"]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["d", [
  "z",
]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]];

console.log(
  xah_is_array_equal(flat_jon(aa, []), flat_xah(aa, 99)),
);

{
  // timing
  const aa = [[["a", ["b", ["k", ["a", ["b", ["c"], [["a", [["a", ["b", ["k", [
    "a",
    ["b", ["c"]],
    ["a", ["x", ["c"], ["a", ["x", ["k"]]], ["d", ["z"]]]],
    ["d", ["m"]],
  ], ["d", ["e"]]]]], ["d", ["e"]]], ["b", ["k", ["a", ["b", ["c"]], ["a", [
    "x",
    ["c"],
    ["a", ["x", ["k"]]],
    ["d", ["z"]],
  ]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["a", ["x", ["c"], ["a", [
    "x",
    ["k"],
  ], [["a", ["b", ["k", ["a", ["b", ["c"]], ["a", ["x", ["c"], ["a", ["x", [
    "k",
  ]]], ["d", ["z"]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["d", [
    "z",
  ]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]];
  const test_repeat_count = 500;
  const start = new Date().getTime();
  for (let i = 0; i < test_repeat_count; ++i) flat_jon(aa, []);
  const end = new Date().getTime();
  // result
  console.log(end - start);
}

{
  // timing
  const aa = [[["a", ["b", ["k", ["a", ["b", ["c"], [["a", [["a", ["b", ["k", [
    "a",
    ["b", ["c"]],
    ["a", ["x", ["c"], ["a", ["x", ["k"]]], ["d", ["z"]]]],
    ["d", ["m"]],
  ], ["d", ["e"]]]]], ["d", ["e"]]], ["b", ["k", ["a", ["b", ["c"]], ["a", [
    "x",
    ["c"],
    ["a", ["x", ["k"]]],
    ["d", ["z"]],
  ]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["a", ["x", ["c"], ["a", [
    "x",
    ["k"],
  ], [["a", ["b", ["k", ["a", ["b", ["c"]], ["a", ["x", ["c"], ["a", ["x", [
    "k",
  ]]], ["d", ["z"]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]], ["d", [
    "z",
  ]]]], ["d", ["m"]]], ["d", ["e"]]]]], ["d", ["e"]]]];
  const test_repeat_count = 500;
  const start = new Date().getTime();
  for (let i = 0; i < test_repeat_count; ++i) flat_xah(aa, 999);
  const end = new Date().getTime();
  // result
  console.log(end - start);
}

// console.log ( flat_jon(aa, []) );
// console.log ( flat_xah(aa, 99) );

// console.log( aa );
// console.log( flat_xah(aa) );
// console.log( flat_xah(aa,1) );
// console.log( flat_xah(aa,2) );
// console.log( flat_xah(aa,3) );
// console.log( flat_xah(aa,4) );
// console.log( flat_xah(aa,400) );

xah_talk_show_2019-10-31.txt