<commit_before>'use strict';

function arrayEquals(before, after) {
  if(before.length !== after.length) {
    return false;
  }
  for(var i=0; i<before.length; i++) {
    if(after[i] !== before[i]) {
      return false;
    }
  }
  return true;
}

module.exports = function generate(before, after) {
  if(before === null || after === null ||
    typeof before !== 'object' || typeof after !== 'object' ||
    Array.isArray(before) !== Array.isArray(after)) {
    return after;
  }

  if(Array.isArray(before)) {
    if(!arrayEquals(before, after)) {
      return after;
    }
    return undefined;
  }

  var patch = {};
  var beforeKeys = Object.keys(before);
  var afterKeys = Object.keys(after);

  // removed elements
  beforeKeys.filter(function(key) {
    return afterKeys.indexOf(key) === -1;
  })
  .forEach(function(key) {
    patch[key] = null;
  });

  // new elements
  afterKeys.filter(function(key) {
    return beforeKeys.indexOf(key) === -1;
  })
  .forEach(function(key) {
    patch[key] = after[key];
  });

  // modified elements
  beforeKeys.filter(function(i) {
    return afterKeys.indexOf(i) !== -1;
  })
  .forEach(function(key) {
    if(before[key] !== null && typeof before[key] === 'object') {
      var subPatch = generate(before[key], after[key]);
      if(subPatch !== undefined) {
        patch[key] = subPatch;
      }
    } else if(before[key] !== after[key]) {
      patch[key] = after[key];
    }
  });
  return (Object.keys(patch).length > 0 ? patch : undefined);
};<commit_msg>Performance optimization on generate()<commit_after>'use strict';

function arrayEquals(before, after) {
  if(before.length !== after.length) {
    return false;
  }
  for(var i=0; i<before.length; i++) {
    if(after[i] !== before[i]) {
      return false;
    }
  }
  return true;
}

module.exports = function generate(before, after) {
  if(before === null || after === null ||
    typeof before !== 'object' || typeof after !== 'object' ||
    Array.isArray(before) !== Array.isArray(after)) {
    return after;
  }

  if(Array.isArray(before)) {
    if(!arrayEquals(before, after)) {
      return after;
    }
    return undefined;
  }

  var patch = {};
  var beforeKeys = Object.keys(before);
  var afterKeys = Object.keys(after);

  var key, i;

  // new elements
  var newKeys = {};
  for(i=0; i < afterKeys.length; i++) {
    key = afterKeys[i];
    if(beforeKeys.indexOf(key) === -1) {
      newKeys[key] = true;
      patch[key] = after[key];
    }
  }

  // removed & modified elements
  var removedKeys = {};
  for(i=0; i < beforeKeys.length; i++) {
    key = beforeKeys[i];
    if(afterKeys.indexOf(key) === -1) {
      removedKeys[key] = true;
      patch[key] = null;
    } else {
      if(before[key] !== null && typeof before[key] === 'object') {
        var subPatch = generate(before[key], after[key]);
        if(subPatch !== undefined) {
          patch[key] = subPatch;
        }
      } else if(before[key] !== after[key]) {
        patch[key] = after[key];
      }
    }
  }

  return (Object.keys(patch).length > 0 ? patch : undefined);
};
{'fraction_non_alphanumeric': 0.17792207792207793, 'fraction_numerical': 0.0035714285714285713, 'mean_word_length': 2.870603015075377, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1724905', 'n_tokens_mistral': 1032, 'n_tokens_neox': 1024, 'n_words': 215}