From 38169bdf1b988be11d025c111294c0866f74adb0 Mon Sep 17 00:00:00 2001 From: Edward Stangler Date: Sun, 16 Jun 2024 04:59:40 -0500 Subject: [PATCH 1/2] Speed up StyleContextStack.autopush() by skipping slower creation of styleOverrideObject and just using the item itself. --- src/styleContextStack.js | 47 +++------------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/src/styleContextStack.js b/src/styleContextStack.js index ee795aa57..6a3318397 100644 --- a/src/styleContextStack.js +++ b/src/styleContextStack.js @@ -84,50 +84,9 @@ StyleContextStack.prototype.autopush = function (item) { this.push(styleNames[i]); } - var styleProperties = [ - 'font', - 'fontSize', - 'fontFeatures', - 'bold', - 'italics', - 'alignment', - 'color', - 'columnGap', - 'fillColor', - 'fillOpacity', - 'decoration', - 'decorationStyle', - 'decorationColor', - 'background', - 'lineHeight', - 'characterSpacing', - 'noWrap', - 'markerColor', - 'leadingIndent', - 'sup', - 'sub' - //'tableCellPadding' - // 'cellBorder', - // 'headerCellBorder', - // 'oddRowCellBorder', - // 'evenRowCellBorder', - // 'tableBorder' - ]; - var styleOverrideObject = {}; - var pushStyleOverrideObject = false; - - styleProperties.forEach(function (key) { - if (!isUndefined(item[key]) && !isNull(item[key])) { - styleOverrideObject[key] = item[key]; - pushStyleOverrideObject = true; - } - }); - - if (pushStyleOverrideObject) { - this.push(styleOverrideObject); - } - - return styleNames.length + (pushStyleOverrideObject ? 1 : 0); + // rather than spend significant time making a styleOverrideObject, just add item + this.push(item); + return styleNames.length + 1; }; /** From d0f6560dd5e9cef880c31edd01a58c8422e61753 Mon Sep 17 00:00:00 2001 From: Edward Stangler Date: Mon, 17 Jun 2024 03:33:28 -0500 Subject: [PATCH 2/2] Adjustments to three tests due to change in StyleContextStack.autopush() behavior for speed. --- tests/styleContextStack.js | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/tests/styleContextStack.js b/tests/styleContextStack.js index 0236578c8..ec4bd7074 100644 --- a/tests/styleContextStack.js +++ b/tests/styleContextStack.js @@ -21,6 +21,36 @@ describe('StyleContextStack', function () { var defaultStyle = { fontSize: 12, bold: false, font: 'Helvetica' }; + var styleProperties = [ + 'font', + 'fontSize', + 'fontFeatures', + 'bold', + 'italics', + 'alignment', + 'color', + 'columnGap', + 'fillColor', + 'fillOpacity', + 'decoration', + 'decorationStyle', + 'decorationColor', + 'background', + 'lineHeight', + 'characterSpacing', + 'noWrap', + 'markerColor', + 'leadingIndent', + 'sup', + 'sub' + //'tableCellPadding' + // 'cellBorder', + // 'headerCellBorder', + // 'oddRowCellBorder', + // 'evenRowCellBorder', + // 'tableBorder' + ]; + var stackWithDefaultStyle; var fullStack; @@ -113,21 +143,25 @@ describe('StyleContextStack', function () { }); describe('autopush', function () { - it('should not push anything if no style nor style-property is defined', function () { - assert.equal(fullStack.autopush({ anotherProperty: 'test' }), 0); + it('should not push any style if no style nor style-property is defined', function () { + assert.equal(fullStack.autopush({ anotherProperty: 'test' }), 1); + assert.equal(fullStack.styleOverrides.length, 1); + styleProperties.forEach(function(key) { + assert.equal(fullStack.styleOverrides[0][key], undefined); + }); }); it('should push style name if object specifies it in the style property', function () { assert.equal(fullStack.styleOverrides.length, 0); - assert.equal(fullStack.autopush({ anotherProperty: 'test', style: 'header' }), 1); - assert.equal(fullStack.styleOverrides.length, 1); + assert.equal(fullStack.autopush({ anotherProperty: 'test', style: 'header' }), 2); + assert.equal(fullStack.styleOverrides.length, 2); assert.equal(fullStack.styleOverrides[0], 'header'); }); it('should push all style names if object specifies them as an array in the style property', function () { assert.equal(fullStack.styleOverrides.length, 0); - assert.equal(fullStack.autopush({ anotherProperty: 'test', style: ['header', 'small'] }), 2); - assert.equal(fullStack.styleOverrides.length, 2); + assert.equal(fullStack.autopush({ anotherProperty: 'test', style: ['header', 'small'] }), 3); + assert.equal(fullStack.styleOverrides.length, 3); assert.equal(fullStack.styleOverrides[0], 'header'); assert.equal(fullStack.styleOverrides[1], 'small'); });