Skip to content

Commit b803f5c

Browse files
authored
[all hosts] (Performance) Update correlated objects pattern article with sample link (#5016)
* Update Correllated objects pattern article with sample link * Change link to prod
1 parent b02a72a commit b803f5c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

docs/concepts/correlated-objects-pattern.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Avoid using the context.sync method in loops
33
description: Learn how to use the split loop and correlated objects patterns to avoid calling context.sync in a loop.
44
ms.topic: best-practice
5-
ms.date: 01/15/2025
5+
ms.date: 01/31/2025
66
ms.localizationpriority: medium
77
---
88

@@ -133,7 +133,9 @@ The preceding example suggests the following procedure for turning a loop that c
133133

134134
Let's consider a more complex scenario where processing the items in the collection requires data that isn't in the items themselves. The scenario envisions a Word add-in that operates on documents created from a template with some boilerplate text. Scattered in the text are one or more instances of the following placeholder strings: "{Coordinator}", "{Deputy}", and "{Manager}". The add-in replaces each placeholder with some person's name. While the UI of the add-in isn't important to this article, the add-in could have a task pane with three text boxes, each labeled with one of the placeholders. The user enters a name in each text box and then presses a **Replace** button. The handler for the button creates an array that maps the names to the placeholders, and then replaces each placeholder with the assigned name.
135135

136-
You don't need to actually produce an add-in with this UI to experiment with the code. You can use the [Script Lab tool](../overview/explore-with-script-lab.md) to prototype the important code. Use the following assignment statement to create the mapping array.
136+
You can use the [Script Lab tool](../overview/explore-with-script-lab.md) to follow along with the code snippets shown here. In Word, you can load the "Correlated objects pattern" sample or [import this sample code from the GitHub repo](https://raw.githubusercontent.com/OfficeDev/office-js-snippets/refs/heads/prod/samples/word/90-scenarios/correlated-objects-pattern.yaml).
137+
138+
The following assignment statement creates the mapping array between placeholder and assigned names.
137139

138140
```javascript
139141
const jobMapping = [
@@ -143,14 +145,14 @@ const jobMapping = [
143145
];
144146
```
145147

146-
The following code shows how you might replace each placeholder with its assigned name if you used `context.sync` inside loops.
148+
The following code shows how you might replace each placeholder with its assigned name if you used `context.sync` inside loops. This corresponds to the `replacePlaceholdersSlow` function in the sample.
147149

148150
```javascript
149151
Word.run(async (context) => {
150152
// The context.sync calls in the loops will degrade performance.
151153
for (let i = 0; i < jobMapping.length; i++) {
152154
let options = Word.SearchOptions.newObject(context);
153-
options.matchWildCards = false;
155+
options.matchWildcards = false;
154156
let searchResults = context.document.body.search(jobMapping[i].job, options);
155157
searchResults.load('items');
156158

@@ -165,15 +167,15 @@ Word.run(async (context) => {
165167
});
166168
```
167169

168-
In the preceding code, there's an outer and an inner loop. Each of them contains a `context.sync` call. Based on the first code snippet in this article, you probably see that the `context.sync` in the inner loop can simply be moved after the inner loop. But that would still leave the code with a `context.sync` (two of them actually) in the outer loop. The following code shows how you can remove `context.sync` from the loops. We discuss the code later.
170+
In the preceding code, there's an outer and an inner loop. Each of them contains a `context.sync` call. Based on the first code snippet in this article, you probably see that the `context.sync` in the inner loop can simply be moved after the inner loop. But that would still leave the code with a `context.sync` (two of them actually) in the outer loop. The following code shows how you can remove `context.sync` from the loops. It corresponds to the `replacePlaceholders` function in the sample. We discuss the code later.
169171

170172
```javascript
171173
Word.run(async (context) => {
172174

173175
const allSearchResults = [];
174176
for (let i = 0; i < jobMapping.length; i++) {
175177
let options = Word.SearchOptions.newObject(context);
176-
options.matchWildCards = false;
178+
options.matchWildcards = false;
177179
let searchResults = context.document.body.search(jobMapping[i].job, options);
178180
searchResults.load('items');
179181
let correlatedSearchResult = {

0 commit comments

Comments
 (0)