Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by using for-of loop instead of iterate functions #79

Merged
merged 7 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ rules:
ignoreTemplateLiterals: true

no-param-reassign: off

no-restricted-syntax:
- error
- ForInStatement
- LabeledStatement
- WithStatement
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Improve conversion performance by using `for-of` loop (40-70% faster) ([#79](https://github.com/marp-team/marpit/pull/79))

## v0.1.2 - 2018-09-20

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Element {
tag: { enumerable: true, value: tag },
})

Object.keys(attributes).forEach(attr => {
for (const attr of Object.keys(attributes)) {
Object.defineProperty(this, attr, {
enumerable: true,
value: attributes[attr],
})
})
}

Object.freeze(this)
}
Expand Down
19 changes: 12 additions & 7 deletions src/helpers/inline_style.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export default class InlineStyle {
* The unexpected declarations will strip to prevent a style injection.
*/
toString() {
return Object.keys(this.decls).reduce((stt, prop) => {
let built = ''

for (const prop of Object.keys(this.decls)) {
let parsed

try {
Expand All @@ -73,14 +75,17 @@ export default class InlineStyle {
})
} catch (e) {
// A declaration that have value it cannot parse will ignore.
return stt
}

parsed.each(node => {
if (node.type !== 'decl' || node.prop !== prop) node.remove()
})
if (parsed) {
parsed.each(node => {
if (node.type !== 'decl' || node.prop !== prop) node.remove()
})

built += `${parsed.toString()};`
}
}

return `${stt}${parsed.toString()};`
}, '')
return built
}
}
28 changes: 15 additions & 13 deletions src/helpers/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
* @returns {Array[]} Splited array.
*/
function split(arr, func, keepSplitedValue = false) {
return arr.reduce(
(ret, value) => {
/**
* Return true at the split point.
*
* @callback splitCallback
* @param {*} value
*/
if (func(value)) return [...ret, keepSplitedValue ? [value] : []]
const ret = [[]]

for (const value of arr) {
/**
* Return true at the split point.
*
* @callback splitCallback
* @param {*} value
*/
if (func(value)) {
ret.push(keepSplitedValue ? [value] : [])
} else {
ret[ret.length - 1].push(value)
return ret
},
[[]]
)
}
}

return ret
}

export default split
14 changes: 5 additions & 9 deletions src/helpers/wrap_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ function wrapTokens(type, container, tokens = []) {
const { tag } = container

// Update nesting level of wrapping tokens
tokens.forEach(t => {
t.level += 1
})
for (const t of tokens) t.level += 1

// Create markdown-it tokens
const open = new Token(`${type}_open`, tag, 1)
Expand All @@ -30,12 +28,10 @@ function wrapTokens(type, container, tokens = []) {
Object.assign(close, { ...(container.close || {}) })

// Assign attributes
Object.keys(container).forEach(attr => {
if (['open', 'close', 'tag'].includes(attr)) return
if (container[attr] == null) return

open.attrSet(attr, container[attr])
})
for (const attr of Object.keys(container)) {
if (!['open', 'close', 'tag'].includes(attr) && container[attr] != null)
open.attrSet(attr, container[attr])
}

return [open, ...tokens, close]
}
Expand Down
138 changes: 70 additions & 68 deletions src/markdown/background_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ function backgroundImage(md) {
'marpit_parse_image',
'marpit_background_image',
({ tokens }) => {
tokens.forEach(t => {
if (t.type !== 'image') return

if (t.meta.marpitImage.options.includes('bg')) {
for (const t of tokens) {
if (t.type === 'image' && t.meta.marpitImage.options.includes('bg')) {
t.meta.marpitImage.background = true
t.hidden = true

t.meta.marpitImage.options.forEach(opt => {
for (const opt of t.meta.marpitImage.options) {
if (bgSizeKeywords[opt])
t.meta.marpitImage.backgroundSize = bgSizeKeywords[opt]

if (opt === 'left' || opt === 'right')
t.meta.marpitImage.backgroundSplit = opt
})
}
}
})
}
}
)

Expand All @@ -57,7 +55,7 @@ function backgroundImage(md) {

let current = {}

tokens.forEach(tb => {
for (const tb of tokens) {
if (tb.type === 'marpit_slide_open') current.open = tb
if (tb.type === 'marpit_inline_svg_content_open')
current.svgContent = tb
Expand Down Expand Up @@ -92,44 +90,44 @@ function backgroundImage(md) {
current = {}
}

if (!current.open || tb.type !== 'inline') return

tb.children.forEach(t => {
if (t.type !== 'image') return

const {
background,
backgroundSize,
backgroundSplit,
filter,
height,
size,
url,
width,
} = t.meta.marpitImage

if (background && !url.match(/^\s*$/)) {
current.images = [
...(current.images || []),
{
if (current.open && tb.type === 'inline')
for (const t of tb.children) {
if (t.type === 'image') {
const {
background,
backgroundSize,
backgroundSplit,
filter,
height,
size: (() => {
const s = size || backgroundSize || undefined

return !['contain', 'cover'].includes(s) && (width || height)
? `${width || s || 'auto'} ${height || s || 'auto'}`
: s
})(),
size,
url,
width,
},
]
}
} = t.meta.marpitImage

if (backgroundSplit) current.split = backgroundSplit
})
})
if (background && !url.match(/^\s*$/)) {
current.images = [
...(current.images || []),
{
filter,
height,
size: (() => {
const s = size || backgroundSize || undefined

return !['contain', 'cover'].includes(s) &&
(width || height)
? `${width || s || 'auto'} ${height || s || 'auto'}`
: s
})(),
url,
width,
},
]
}

if (backgroundSplit) current.split = backgroundSplit
}
}
}
}
)

Expand All @@ -138,10 +136,9 @@ function backgroundImage(md) {
'marpit_advanced_background',
state => {
let current
const newTokens = []

state.tokens = state.tokens.reduce((ret, t) => {
let tokens = [t]

for (const t of state.tokens) {
if (
t.type === 'marpit_inline_svg_content_open' &&
t.meta &&
Expand All @@ -160,7 +157,7 @@ function backgroundImage(md) {
if (splitSide === 'left') t.attrSet('x', '50%')
}

tokens = [
newTokens.push(
...wrapTokens(
'marpit_advanced_background_foreign_object',
{ tag: 'foreignObject', width, height },
Expand All @@ -178,27 +175,30 @@ function backgroundImage(md) {
tag: 'div',
'data-marpit-advanced-background-container': true,
},
images.reduce(
(imgArr, img) => [
...imgArr,
...wrapTokens('marpit_advanced_background_image', {
tag: 'figure',
style: [
`background-image:url("${img.url}");`,
img.size && `background-size:${img.size};`,
img.filter && `filter:${img.filter};`,
]
.filter(s => s)
.join(''),
}),
],
[]
)
(() => {
const imageTokens = []

for (const img of images)
imageTokens.push(
...wrapTokens('marpit_advanced_background_image', {
tag: 'figure',
style: [
`background-image:url("${img.url}");`,
img.size && `background-size:${img.size};`,
img.filter && `filter:${img.filter};`,
]
.filter(s => s)
.join(''),
})
)

return imageTokens
})()
)
)
),
t,
]
t
)
} else if (current && t.type === 'marpit_inline_svg_content_close') {
const { open, height, width } = current.meta.marpitBackground

Expand All @@ -212,7 +212,7 @@ function backgroundImage(md) {
)
style.set('color', open.meta.marpitDirectives.color)

tokens = [
newTokens.push(
t,
...wrapTokens(
'marpit_advanced_background_foreign_object',
Expand All @@ -231,14 +231,16 @@ function backgroundImage(md) {
'data-marpit-pagination'
),
})
),
]
)
)

current = undefined
} else {
newTokens.push(t)
}
}

return [...ret, ...tokens]
}, [])
state.tokens = newTokens
}
)
}
Expand Down
3 changes: 1 addition & 2 deletions src/markdown/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ function container(md, containers) {
md.core.ruler.push('marpit_containers', state => {
if (state.inlineMode) return

target.forEach(cont => {
for (const cont of target)
state.tokens = wrapTokens('marpit_containers', cont, state.tokens)
})
})
}

Expand Down
Loading