Skip to content

Commit

Permalink
Binary search for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pwfisher committed Oct 26, 2016
1 parent cdb2143 commit 1c30fc9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
28 changes: 18 additions & 10 deletions dist/shave.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,30 @@ function shave(target, maxHeight, opts) {
if (el.offsetHeight < maxHeight) break;

var fullText = el.textContent;
var trimmedText = fullText;
var lastSpace = void 0;

do {
lastSpace = trimmedText.lastIndexOf(' ');
if (lastSpace < 0) break; // single word is too tall, do nothing
trimmedText = trimmedText.slice(0, lastSpace);
el.textContent = trimmedText;
var words = fullText.split(' ');

// If 0 or 1 words, we're done
if (words.length < 2) break;

// Binary search for number of words which can fit in allotted height
var max = words.length - 1;
var min = 0;
var pivot = void 0;
while (min < max) {
pivot = (min + max + 1) >> 1;
el.textContent = words.slice(0, pivot).join(' ');
el.insertAdjacentHTML('beforeend', charHtml);
} while (el.offsetHeight > maxHeight);
if (el.offsetHeight > maxHeight) max = pivot - 1;else min = pivot;
}

var diff = fullText.slice(lastSpace);
el.textContent = words.slice(0, max).join(' ');
el.insertAdjacentHTML('beforeend', charHtml);
var diff = words.slice(max + 1).join(' ');

el.insertAdjacentHTML('beforeend', '<span class="' + classname + '" style="display:none;">' + diff + '</span>');
}
}

var plugin = window.$ || window.jQuery || window.Zepto;
if (plugin) {
plugin.fn.extend({
Expand Down
29 changes: 19 additions & 10 deletions src/shave.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,32 @@ export default function shave(target, maxHeight, opts) {
if (el.offsetHeight < maxHeight) break;

const fullText = el.textContent;
let trimmedText = fullText;
let lastSpace;

do {
lastSpace = trimmedText.lastIndexOf(' ');
if (lastSpace < 0) break; // single word is too tall, do nothing
trimmedText = trimmedText.slice(0, lastSpace);
el.textContent = trimmedText;
const words = fullText.split(' ');

// If 0 or 1 words, we're done
if (words.length < 2) break;

// Binary search for number of words which can fit in allotted height
let max = words.length - 1;
let min = 0;
let pivot;
while (min < max) {
pivot = (min + max + 1) >> 1;
el.textContent = words.slice(0, pivot).join(' ');
el.insertAdjacentHTML('beforeend', charHtml);
} while (el.offsetHeight > maxHeight);
if (el.offsetHeight > maxHeight) max = pivot - 1;
else min = pivot;
}

const diff = fullText.slice(lastSpace);
el.textContent = words.slice(0, max).join(' ');
el.insertAdjacentHTML('beforeend', charHtml);
const diff = words.slice(max + 1).join(' ');

el.insertAdjacentHTML('beforeend',
`<span class="${classname}" style="display:none;">${diff}</span>`);
}
}

const plugin = window.$ || window.jQuery || window.Zepto;
if (plugin) {
plugin.fn.extend({
Expand Down

0 comments on commit 1c30fc9

Please sign in to comment.