<commit_before>(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define([], factory); // AMD. Register as an anonymous module.
  } else if (typeof exports === 'object') {
    module.exports = factory(); // NodeJS
  } else { // Browser globals (root is window)
  root.flyview = factory();
  }
}(this, function () {

function isStream(obj) {
  return typeof obj === 'function' || false;
}

function isString(s) {
  return typeof s === 'string';
}

function isPrimitive(v) {
  return typeof v === 'string' || typeof v === 'number';
}

var isArray = Array.isArray;

function isMapper(o) {
  return o && o.createFn;
}

// Constants
var MOVE = 0, ADD = 1, REMOVE = 2;

function Mapper(stream, createFn, keyProp) {
  this.stream = stream;
  this.container = null;
  this.createFn = createFn;
  this.keyProp = keyProp;
  this.oldList = [];
  this.oldKeyToElm = {};
}

Mapper.prototype.attach = function(container, s) {
  this.container = container;
  this.stream.map(this.update.bind(this));
};

Mapper.prototype.getKey = function(v) {
  return isString(this.keyProp) ? v[this.keyProp] : v;
};

Mapper.prototype.update = function(list) {
  var i, container = this.container,
      parent = container.parentNode, placeholder,
      frag = document.createDocumentFragment(),
      children = [];
  for (i = 0; i < container.children.length; ++i) {
    children.push(container.children[i]);
  }
  if (parent !== null) {
    placeholder = document.createComment('');
    parent.replaceChild(placeholder, container);
  }
  var lastAppendedTo, // Index of last appended to
      actions = this.diff(list);
  for (i = 0; i < actions.length; ++i) {
    var a = actions[i];
    if (a.type === REMOVE) {
      container.removeChild(children[a.idx]);
    } else {
      if (lastAppendedTo === undefined) {
        lastAppendedTo = a.to;
      } else if (a.to !== lastAppendedTo) {
        container.insertBefore(frag, children[lastAppendedTo]);
        lastAppendedTo = a.to;
      }
      if (a.type === ADD) {
        frag.appendChild(this.createFn(list[a.elm], this.stream));
      } else {
        frag.appendChild(children[a.from]);
      }
    }
  }
  if (frag.children.length > 0) {
    container.insertBefore(frag, children[lastAppendedTo]);
  }
  if (placeholder !== undefined) {
    parent.replaceChild(container, placeholder);
  }
  return actions;
};

Mapper.prototype.diff = function(list) {
  var aIdx = this.oldKeyToElm,
      bIdx = {},
      a = this.oldList,<commit_msg>Improve performance by not detaching container if few modifications<commit_after>(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define([], factory); // AMD. Register as an anonymous module.
  } else if (typeof exports === 'object') {
    module.exports = factory(); // NodeJS
  } else { // Browser globals (root is window)
  root.flyview = factory();
  }
}(this, function () {

function isStream(obj) {
  return typeof obj === 'function' || false;
}

function isString(s) {
  return typeof s === 'string';
}

function isPrimitive(v) {
  return typeof v === 'string' || typeof v === 'number';
}

var isArray = Array.isArray;

function isMapper(o) {
  return o && o.createFn;
}

// Constants
var MOVE = 0, ADD = 1, REMOVE = 2;

function Mapper(stream, createFn, keyProp) {
  this.stream = stream;
  this.container = null;
  this.createFn = createFn;
  this.keyProp = keyProp;
  this.oldList = [];
  this.oldKeyToElm = {};
}

Mapper.prototype.attach = function(container, s) {
  this.container = container;
  this.stream.map(this.update.bind(this));
};

Mapper.prototype.getKey = function(v) {
  return isString(this.keyProp) ? v[this.keyProp] : v;
};

Mapper.prototype.update = function(list) {
  var i, container = this.container,
      parent = container.parentNode, placeholder,
      frag = document.createDocumentFragment(),
      children = [],
      lastAppendedTo, // Index of last appended to
      actions = this.diff(list);
  for (i = 0; i < container.children.length; ++i) {
    children.push(container.children[i]);
  }
  if (parent !== null && actions.length * 5 > children.length) {
    placeholder = document.createComment('');
    parent.replaceChild(placeholder, container);
  }
  for (i = 0; i < actions.length; ++i) {
    var a = actions[i];
    if (a.type === REMOVE) {
      container.removeChild(children[a.idx]);
    } else {
      if (lastAppendedTo === undefined) {
        lastAppendedTo = a.to;
      } else if (a.to !== lastAppendedTo) {
        container.insertBefore(frag, children[lastAppendedTo]);
        lastAppendedTo = a.to;
      }
      if (a.type === ADD) {
        frag.appendChild(this.createFn(list[a.elm], this.stream));
      } else {
        frag.appendChild(children[a.from]);
      }
    }
  }
  if (frag.children.length > 0) {
    container.insertBefore(frag, children[lastAppendedTo]);
  }
  if (placeholder !== undefined) {
    parent.replaceChild(container, placeholder);
  }
  return actions;
};

Mapper.prototype.diff = function(list) {
  var aIdx = this.oldKeyToElm,
      bIdx = {},
      a = this.oldList,
{'fraction_non_alphanumeric': 0.15411764705882353, 'fraction_numerical': 0.0025490196078431374, 'mean_word_length': 3.1743044189852703, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '5948154', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1637, 'n_words': 383}