Message from JavaScript discussions

August 2017

— Only problem is arrays


The algorithms rely on tail edge preemption that lets them make decisions right before hitting the last node, one of which happens to be returning a value

— When it makes the calculation to tell if it's the last node, it does:

if ((!state.isContainer || state.skipNode) && (state.iterations === state.length && nodeStack.length === 0))

Message permanent page

— It says "if the current prop is not a container (not traversable), or has been marked as skipped (for instance if we saw it before), AND our iterations match the length of our search index AND we don't have any more nodes to traverse"

Message permanent page

— The problem is present when you use an array, say with a single element, that does not neccesarily indicate there is a length of 1

Message permanent page

— For an array with something at index 2 and nothing else, I'll have a length of 3... but only one possible iteration, and thus the conditional fails

Message permanent page

— Example of this failure:

var obj = ["one", "two", "three"];

var search = [];

search[2] = "three";

differentia.diff(obj, obj, search);  // undefined

Message permanent page

— This should return false, however... it only goes to index 2, as it's supposed to, and thus even though our search index only has one element, the iterations never reach 3, only 1, making the preemtion fail, and thus it never returns

Message permanent page

— The problem can be averted by using this regular object as a search index instead of an array:

var search = {2:"it doesn't matter what the value is if it's a primitive lol"};

Message permanent page

— Thoughts?

— Well, there must be a way to detect empty array slots

— Is js better than php?